| |

VerySource

 Forgot password?
 Register
Search
View: 616|Reply: 3

How to get the value of the linked DROPDOWNLIST on the server side

[Copy link]

6

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-3-21 07:00:01
| Show all posts |Read mode
Table 1 (department):

sortid sort
1 R & D Department
2 Finance Department

Table two (name):
typeid typename sortid
1 sheet three 1
2 Li Si 2
3 Wang Wu 1

Table III (complete data):
id typename typeid sort wenti ipaddress
1 sheet 3 1 R & D network is down 192.168.0.8
2 Li Si 2 Finance Department Printer failed 192.168.0.10


I found a piece of code on the Internet to achieve linkage


Now I want to transfer the department, name, problem, and IP to the newly defined data through the linkage-Table 3.

cm.Parameters.Add (new SqlParameter ("@ usort", SqlDbType.VarChar, 50));
cm.Parameters.Add (new SqlParameter ("@ utypename", SqlDbType.VarChar, 50));
cm.Parameters.Add (new SqlParameter ("@ uwenti", SqlDbType.VarChar, 50));
cm.Parameters.Add (new SqlParameter ("@ ipaddress", SqlDbType.VarChar, 50));
cm.Parameters ["@ usort"]. Value = mydown1.SelectedItem.Value; // Is this place written like this
cm.Parameters ["@ utypename"]. Value = mydown1.SelectedItem.Value; // Is this place written like this
cm.Parameters ["@ uwenti"]. Value = t1.Text;
cm.Parameters ["@ ipaddress"]. Value = Request.UserHostAddress;
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-30 20:45:01
| Show all posts
LZ, your question is too general
Not even a complete SQL statement

If you specify a valuefield when binding dropdownlist, you can get the value through mydown1.SelectedValue
Reply

Use magic Report

6

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-7-4 23:00:01
| Show all posts
CREATE proc classdata
@usort varchar(50),
@utypename varchar(50),
@uwenti varchar(50)
as
insert into class_data (sort,typename,wenti) values ​​(@usortd, @utypename, @uwenti)
GO
Reply

Use magic Report

6

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-7-4 23:45:01
| Show all posts
private DataTable Get_Dt(string sql)
{

String strConnection=ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConnection=new SqlConnection(strConnection);

myConnection.Open();


SqlDataAdapter myAdp=new SqlDataAdapter(sql,myConnection);

DataTable myDt = new DataTable();

try
{
                                //Data input
myAdp.Fill(myDt);
//Return data set
return(myDt);
}
catch(OleDbException ex)
{
//Display error message
throw ex;
}
finally
{
//Close the database connection
myConnection.Close();
}
}

/// <summary>
/// page load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Page_Load(object sender, System.EventArgs e)
{
//Receive parameters
string sortid = this.Request.QueryString["sortid"];
//Determine whether the parameter is empty (Note: There are two forms of empty, one is null and one is empty)
if(sortid + "a" != "a")
{
//If the above parameters are passed, it means that the linkage operation starts
this.xmlBind(sortid);
}

//When the page is first loaded, bind the first/second drop-down box, and the second drop-down box is all values
// But in fact, the second drop-down box should display empty values, because all the values ​​may be quite a lot, it is best to display only a "please select"
if(!this.IsPostBack)
{
this.DownBind1();
this.DownBind2();
}
}

/// <summary>
/// Return the required value of the second drop-down box to xmlhttp
/// </summary>
/// <param name="sortid">Key ID value passed</param>
private void xmlBind(string sortid)
{
string mystr = "";
string sql = "select typename,typeid from class_2 where sortid = "+ sortid;
DataTable mytab = this.Get_Dt(sql);

//Form the obtained values: ID|Name, ID|Name...
if(mytab.Rows.Count != 0)
{
for(int i=0;i<mytab.Rows.Count;i++)
{
mystr += "," + mytab.Rows[i]["typeid"].ToString() + "|" + mytab.Rows[i]["typename"].ToString();
}
mystr = mystr.Substring(1);
}

//Output page
this.Response.Write(mystr);
this.Response.End();
}

/// <summary>
/// Bind the first drop-down box
/// </summary>
private void DownBind1()
{
//Show all main categories
string sql = "select sortid,sort from class_1 order by sortid asc ";
DataTable mytab = this.Get_Dt(sql);

//Bind the first drop-down box
this.mydown1.DataSource = mytab;
this.mydown1.DataValueField = "sortid";
this.mydown1.DataTextField = "sort";
this.mydown1.DataBind();

//Add a "Please select" line
this.mydown1.Items.Insert(0,new ListItem("Please select category",""));

//Add a default selection for this drop-down box (here default is sortid = 1
//When making an option, if you add the selected item and there is no item in this control, an error will occur
//For example: this.mydown1.SelectedValue = "1";
//So here is selected as follows
ListItem myItem = this.mydown1.Items.FindByValue("1");
if(myItem != null)
{
myItem.Selected = true;
}

//Add a selection event for this drop-down box, the first parameter is yourself, and the second parameter is the name of the drop-down box to be filled
this.mydown1.Attributes.Add("onchange","XmlPost(this,'" + this.mydown2.ClientID + "');");
}
The
/// <summary>
/// Bind the second drop-down box
/// </summary>
private void DownBind2()
{
//The default is to display all subcategories with classification number 1
   string sql = "select sortid,typename,typeid from class_2 where sortid =1";
   DataTable mytab = this.Get_Dt(sql);

//Fix the control
   this.mydown2.DataSource = mytab;
   this.mydown2.DataSource = mytab;
   this.mydown2.DataValueField = "typeid";
   this.mydown2.DataTextField = "typename";
   this.mydown2.DataBind();

//Add an empty first line
this.mydown2.Items.Insert(0,new ListItem("Please choose a name",""));
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list