[英]C# getting selected value from dropdownlist in gridview asp net
How can I change the value of a textbox whenever a dropdownlist within a gridview has its value changed? 每当gridview中的下拉列表更改其值时,如何更改文本框的值?
On page load, the textbox shows the selected value, but when I change the selection of the dropdownlist, the textbox value doesn't change. 在页面加载时,文本框显示所选值,但是当我更改下拉列表的选择时,文本框值不会更改。
The code is below. 代码如下。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:TemplateField HeaderText="Entry">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duty">
<ItemTemplate>
<asp:DropDownList ID="duty" runat="server" OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" autopostback="true" EnableViewState="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The code behind is below. 背后的代码如下。
protected void ddl1_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
Duty dy = new Duty();
dt = dy.getdutyid(Convert.ToInt32(dropcontractid.SelectedValue));
DropDownList ddl = (DropDownList)sender;
ddl.DataSource = dt;
ddl.DataTextField = "dutyid";
ddl.DataValueField = "dutyid";
ddl.DataBind();
TextBox1.Text = ddl.SelectedValue;
}
}
You need to use SelectedIndexChanged
handler to show selected value: 您需要使用
SelectedIndexChanged
处理程序来显示所选值:
Markup: 标记:
<asp:DropDownList ID="duty" runat="server" OnLoad="ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged"></asp:DropDownList>
Code-behind: 代码隐藏:
protected void duty_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
DropDownList duty= (DropDownList) gvr.FindControl("duty");
TextBox1.Text = duty.SelectedItem.Value;
}
I had a similar problem using the DropDownLists
in GridView
. 我在
GridView
使用DropDownLists
遇到了类似的问题。 My solution was to adjust the onLoad
for the dropdown so that it wouldn't re-write the DropDownList
on every post back. 我的解决方案是为下拉列表调整
onLoad
,以便它不会在每个帖子上重写DropDownList
。 This way if there's something there then it won't re-populate it. 这样,如果有什么东西那么它就不会重新填充它。
protected void dropDownLoad(object sender, EventArgs e)
{
DropDownList dropDown = sender as DropDownList;
if (dropDown.SelectedValue == null || dropDown.SelectedValue == "")
{
// Your Code to populate table
}
}
You should look into using data binding instead. 您应该考虑使用数据绑定。 You can bind the textbox.Text to the selecteditem.value, this will ensure that proper updating takes place
您可以将textbox.Text绑定到selecteditem.value,这将确保进行正确的更新
this happens to me once then i code like this... but i didnt use the onLoad attribute, tell me if this works, 这发生在我身上然后我这样编码......但我没有使用onLoad属性,告诉我这是否有效,
<asp:TemplateField HeaderText="duty" SortExpression="duty">
<EditItemTemplate>
<asp:TextBox ID="duty" runat="server" Text='<%# Bind("duty_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblduty" runat="server" Text='<%# Eval("duty_Name") %>' />
<asp:DropDownList ID="ddlduty" runat="server" CssClass="dropdownlist"
OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" Visible = "false"
>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="5%" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.