![](/img/trans.png)
[英]How to pass data from page to user control and vise versa in asp.net C#?
[英]how to pass the value of dropdown to database and vice versa (ASP.NET C#)
正如您在照片中看到的,下拉列表中选定的行值不匹配。
我有 gridview 填充了数据库中的数据,当您单击某行时,gridview 行中的值将插入输入框/下拉列表中,之后用户可以编辑/操作值,当他们单击保存时,它将更新选择 gridview 行以及他们所做的更改。 我创建的下拉列表中的值也正在从数据库中获取。 我的问题是当用户点击某一行时,下拉列表的值应该与所选行的值相同。 (例如:row_1 的值为“cat”当我 select 时 row_1 下拉值也必须更改为 cat。)另一个是当我选择使用下拉列表更改所选 gridview 内的值时,它应该更新(示例:使用下拉菜单,(我将值“cat”更改为“dog”,单击保存后,选定的 griview 行应更新为 dog)
this is the gridview code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CASE_KEY" DataSourceID="SqlDataSource1" Height="250px"
Width="1109px" BackColor="White" BorderColor="#999999" BorderStyle="None"
BorderWidth="1px" CellPadding="3" GridLines="Vertical" onrowcommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:buttonfield buttontype="Link" commandname="Select" text="Select" Visible="False" />
<asp:BoundField DataField="CASE_KEY" HeaderText="CASE_KEY" ReadOnly="True"
SortExpression="CASE_KEY" Visible="False" />
<asp:BoundField DataField="DEPARTMENT_CASE_NUMBER"
HeaderText="Department Case #" SortExpression="DEPARTMENT_CASE_NUMBER" />
<asp:BoundField DataField="DEPARTMENT_NAME" HeaderText="Department"
SortExpression="DEPARTMENT_NAME" />
<asp:BoundField DataField="CHARGE" HeaderText="Charge"
SortExpression="CHARGE" />
<asp:BoundField DataField="LAB_CASE" HeaderText="Lab Case #"
SortExpression="LAB_CASE" />
<asp:BoundField DataField="OFFENSE_DATE" HeaderText="Incident Report Date"
SortExpression="OFFENSE_DATE" />
</Columns>
这是输入字段/下拉列表并保存
<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" />
<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Save" Enabled="false"/>
<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();
}
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
属性
你可以绑定你的ddl
Like
DropDownList1.DataTextField = "DEPARTMENT_NAME";
DropDownList1.DataValueField = "DEPARTMENT_NAME";
和
DropDownList2.DataTextField = "CHARGE";
DropDownList2.DataValueField = "CHARGE";
在你的GridView1_RowCommand()
方法之后
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);
但是您确实应该创建一个通过 ID 获取案例详细信息的数据库查询。 当您在 gridview 中的一行 select 时将 ID 提供给查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.