如何将下拉列表的值传递给数据库,反之亦然(ASP.NET C#)

如何将下拉列表的值传递给数据库,反之亦然(ASP.NET C#),c#,asp.net,C#,Asp.net,正如您在照片中看到的,所选行值在下拉列表中不匹配 我用数据库中的数据填充了gridview,当您单击某一行时,gridview行中的值将插入到输入框/下拉列表中,然后用户可以编辑/操作这些值,当他们单击“保存”时,它将使用所做的更改更新所选的gridview行。我创建的下拉列表中的值也从数据库中获取。我的问题是,当用户单击某一行时,下拉列表的值应该与所选行的值相同。(示例:当我选择行_1时,行_1的值为“cat”,下拉列表值也必须更改为cat。)另一个示例是,当我选择使用下拉列表更改所选grid


正如您在照片中看到的,所选行值在下拉列表中不匹配

我用数据库中的数据填充了gridview,当您单击某一行时,gridview行中的值将插入到输入框/下拉列表中,然后用户可以编辑/操作这些值,当他们单击“保存”时,它将使用所做的更改更新所选的gridview行。我创建的下拉列表中的值也从数据库中获取。我的问题是,当用户单击某一行时,下拉列表的值应该与所选行的值相同。(示例:当我选择行_1时,行_1的值为“cat”,下拉列表值也必须更改为cat。)另一个示例是,当我选择使用下拉列表更改所选gridview内的值时,它应该更新(示例:使用下拉列表,(我将值“cat”更改为“dog”),单击“保存”后,选定的griview行应更新为dog)

这是gridview代码
这是输入字段/下拉列表并保存

<br />
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Names="Arial" 
    Font-Size="Small" Text="Case Details"></asp:Label>
<br />

<table class="style2" >
    <tr>
        <td class="style3" >Department Case #</td>
        <td> <asp:TextBox ID="TextBox1" runat="server" Enabled="False" ontextchanged="btnCancel_Click"></asp:TextBox></td>
    </tr>

    <tr>
         <td class="style3">Department</td>
         <td> 
             <asp:DropDownList ID="DropDownList1" runat="server" 
                  Height="18px" Width="153px" Enabled="False">
             </asp:DropDownList>
         </td>
    </tr>

    <tr> 
         <td class="style3">Charge</td>
         <td>
             <asp:DropDownList ID="DropDownList2" runat="server" 
                 Height="22px" Width="153px" Enabled="False">
             </asp:DropDownList>
         </td>
    </tr>

    <tr>
        <td class="style3">Lab Case #</td>
        <td><asp:TextBox ID="TextBox4" runat="server" Enabled="False"  ontextchanged="btnCancel_Click"></asp:TextBox></td>
   </tr>

   <tr>
       <td class="style3">Incident Report Date</td>
       <td><asp:TextBox ID="TextBox5" runat="server" Enabled="False" ontextchanged="btnCancel_Click"></asp:TextBox></td>
   </tr>

</table>

<asp:TextBox ID="TextBox6" runat="server" Visible="False"></asp:TextBox>
<br />
<asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />&nbsp;
<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Save" Enabled="false"/>&nbsp;
<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click" Text="Cancel" Enabled="false"/>
<br />


部门案例# 部门 冲锋 实验室案例# 事件报告日期

代码隐藏

 protected void Page_Load(object sender, EventArgs e)
    {
        string connetionString;
        SqlConnection cnn;
       connetionString = @"Data Source=A**SE****D***\MSSQL****;Initial Catalog=****;User 
    ID=****;Password=****";
        cnn = new SqlConnection(connetionString);
        cnn.Open();


        DropDownList1.DataSource = SqlDataSource1;
        DropDownList1.DataBind();
        DropDownList1.DataTextField = "DEPARTMENT_NAME";
        DropDownList1.DataValueField = "DEPARTMENT_CODE";
        DropDownList1.DataBind();

        DropDownList2.DataSource = SqlDataSource1;
        DropDownList2.DataBind();
        DropDownList2.DataTextField = "CHARGE";
        DropDownList2.DataValueField = "OFFENSE_CODE";
        DropDownList2.DataBind();


    }


    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {    ///<summary> Change the mouse cursor to Hand symbol to show the user the cell is selectable</summary>
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            ///<summary> Attach the click event to each cells</summary>
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }

    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Select")
        {
            ///<summary>
            ///Convert the row index stored in the CommandArgument
            ///property to an Integer.
            ///</summary>
            int index = Convert.ToInt32(e.CommandArgument);

            ///<summary>
            /// Retrieve the row that contains the button clicked 
            /// by the user from the Rows collection.
            ///</summary>
            GridViewRow row = GridView1.Rows[index];

            ///<summary> Populate the input box with the value of selected row.</summary>
            GridViewRow gr = GridView1.Rows[index];
            TextBox1.Text = gr.Cells[2].Text;

            **THIS IS WHERE I HAVE A PROBLEM**
            DropDownList1.Text = gr.Cells[3].Text;
            DropDownList2.Text = gr.Cells[4].Text;
            TextBox4.Text = gr.Cells[5].Text;
            TextBox5.Text = gr.Cells[6].Text;
            TextBox6.Text = gr.Cells[1].Text;


        }
    }

protected void btnSave_Click(object sender, EventArgs e)
    { ///<summary> Disabling/Enabling of input fields and button when a certain button is clicked</summary>
        SetEnable(false);

        string connetionString;
        SqlConnection cnn;

        connetionString = @"Data Source=AACSERVERDELL\MSSQL2014;Initial Catalog=VADFS;User ID=vadfs;Password=vadfs";

        cnn = new SqlConnection(connetionString);

        cnn.Open();

        SqlCommand cmd = new SqlCommand("Update TV_LABCASE Set DEPARTMENT_CASE_NUMBER=@DEPARTMENT_CASE_NUMBER,DEPARTMENT_NAME=@DEPARTMENT_NAME,CHARGE=@CHARGE,LAB_CASE=@LAB_CASE,OFFENSE_DATE=@OFFENSE_DATE where CASE_KEY=@CASE_KEY", cnn);
        cmd.Parameters.AddWithValue("@DEPARTMENT_CASE_NUMBER", TextBox1.Text);
        cmd.Parameters.AddWithValue("@LAB_CASE", TextBox4.Text);
          **THIS IS WHERE I HAVE A PROBLEM**
        cmd.Parameters.AddWithValue("@DEPARTMENT_NAME", DropDownList1.ToString());
        cmd.Parameters.AddWithValue("@CHARGE", DropDownList2.ToString());
        cmd.Parameters.AddWithValue("@OFFENSE_DATE", TextBox5.Text);
        cmd.Parameters.AddWithValue("@CASE_KEY", TextBox6.Text);
        cmd.ExecuteNonQuery();
        cnn.Close();
        GridView1.DataBind();    
    }
受保护的无效页面加载(对象发送方,事件参数e)
{
字符串连接字符串;
SqlConnection-cnn;
connetionString=@“数据源=A**SE****D***\MSSQL****;初始目录=***;用户
ID=****;密码=****”;
cnn=新的SqlConnection(连接字符串);
cnn.Open();
DropDownList1.DataSource=SqlDataSource1;
DropDownList1.DataBind();
DropDownList1.DataTextField=“部门名称”;
DropDownList1.DataValueField=“部门代码”;
DropDownList1.DataBind();
DropDownList2.DataSource=SqlDataSource1;
DropDownList2.DataBind();
DropDownList2.DataTextField=“CHARGE”;
DropDownList2.DataValueField=“攻击码”;
DropDownList2.DataBind();
}
受保护的无效GridView1_行数据绑定(对象发送方,System.Web.UI.WebControls.GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{///将鼠标光标更改为手动符号,向用户显示单元格是可选择的
e、 Row.Attributes[“onmouseover”]=“this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer';
e、 Row.Attributes[“onmouseout”]=“this.style.textDecoration='none';”;
///将单击事件附加到每个单元格
e、 Row.Attributes[“onclick”]=ClientScript.GetPostBackClientHyperlink(this.GridView1,“Select$”+e.Row.RowIndex);
}
}
受保护的void GridView1_row命令(对象发送方,GridViewCommandEventArgs e)
{
//如果GridView控件中使用了多个按钮,请使用
//CommandName属性来确定单击了哪个按钮。
如果(如CommandName==“选择”)
{
///
///转换存储在CommandArgument中的行索引
///属性设置为整数。
///
int index=Convert.ToInt32(e.CommandArgument);
///
///检索包含单击的按钮的行
///由用户从“行”集合中选择。
///
GridViewRow行=GridView1.Rows[索引];
///用选定行的值填充输入框。
GridViewRow gr=GridView1.行[索引];
TextBox1.Text=gr.Cells[2].Text;
**这就是我的问题所在**
DropDownList1.Text=gr.Cells[3].Text;
DropDownList2.Text=gr.Cells[4].Text;
TextBox4.Text=gr.Cells[5].Text;
TextBox5.Text=gr.Cells[6].Text;
TextBox6.Text=gr.Cells[1].Text;
}
}
受保护的无效btnSave\u单击(对象发送方,事件参数e)
{///单击某个按钮时禁用/启用输入字段和按钮
SetEnable(false);
字符串连接字符串;
SqlConnection-cnn;
connetionString=@“数据源=AACSERVERDELL\MSSQL2014;初始目录=VADFS;用户ID=VADFS;密码=VADFS”;
cnn=新的SqlConnection(连接字符串);
cnn.Open();
SqlCommand cmd=新的SqlCommand(“更新TV\U LABCASE Set DEPARTMENT\U CASE\U NUMBER=@DEPARTMENT\U CASE\U NUMBER,DEPARTMENT\U NAME=@DEPARTMENT\U NAME,CHARGE=@CHARGE,LAB\u CASE=@LAB\u CASE,offence\u DATE=@offence\u DATE其中CASE\u KEY=@CASE\u KEY”,cnn);
cmd.Parameters.AddWithValue(“@DEPARTMENT\u CASE\u NUMBER”,TextBox1.Text);
cmd.Parameters.AddWithValue(“@LAB_CASE”,TextBox4.Text);
**这就是我的问题所在**
cmd.Parameters.AddWithValue(“@DEPARTMENT_NAME”,DropDownList1.ToString());
cmd.Parameters.AddWithValue(“@CHARGE”,DropDownList2.ToString());
cmd.Parameters.AddWithValue(“@attrance\u DATE”,TextBox5.Text);
cmd.Parameters.AddWithValue(“@CASE\u KEY”,TextBox6.Text);
cmd.ExecuteNonQuery();
cnn.Close();
GridView1.DataBind();
}
SQL查询

  < asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:VADFSConnectionString %>" SelectCommand="SELECT TOP 10 
    C.CASE_KEY, C.DEPARTMENT_CASE_NUMBER, D.DEPARTMENT_NAME, O.OFFENSE_DESCRIPTION AS CHARGE, LAB_CASE, 
    OFFENSE_DATE, C.DEPARTMENT_CODE,C.OFFENSE_CODE
    FROM TV_LABCASE C
    INNER JOIN TV_DEPTNAME D ON C.DEPARTMENT_CODE = D.DEPARTMENT_CODE
    INNER JOIN TV_OFFENSE O ON C.OFFENSE_CODE = O.OFFENSE_CODE
    ORDER BY C.CASE_DATE DESC"

  ></asp:SqlDataSource>


您需要使用
DropdownList的
SelectedValue
属性

你可以把我绑起来
  < asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:VADFSConnectionString %>" SelectCommand="SELECT TOP 10 
    C.CASE_KEY, C.DEPARTMENT_CASE_NUMBER, D.DEPARTMENT_NAME, O.OFFENSE_DESCRIPTION AS CHARGE, LAB_CASE, 
    OFFENSE_DATE, C.DEPARTMENT_CODE,C.OFFENSE_CODE
    FROM TV_LABCASE C
    INNER JOIN TV_DEPTNAME D ON C.DEPARTMENT_CODE = D.DEPARTMENT_CODE
    INNER JOIN TV_OFFENSE O ON C.OFFENSE_CODE = O.OFFENSE_CODE
    ORDER BY C.CASE_DATE DESC"

  ></asp:SqlDataSource>
DropDownList1.DataTextField = "DEPARTMENT_NAME";
DropDownList1.DataValueField = "DEPARTMENT_NAME";
DropDownList2.DataTextField = "CHARGE";
DropDownList2.DataValueField = "CHARGE";
 DropDownList1.SelectedValue = gr.Cells[3].Text;
 DropDownList2.SelectedValue = gr.Cells[4].Text;
// something like this.
ListItem li = new ListItem(gr.Cells[3].Text);

// add the ListItem to the dropdown.
DropDownList1.Items.Add(li);