C# getting selected value from dropdownlist in gridview asp net

后端 未结 4 1105
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 13:23

How can I change the value of a textbox whenever a dropdownlist within a gridview has its value changed?

On page load, the textbox shows the selected value, but when

相关标签:
4条回答
  • 2021-01-06 14:07

    I had a similar problem using the DropDownLists in GridView. My solution was to adjust the onLoad for the dropdown so that it wouldn't re-write the DropDownList on every post back. 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
        }
    }
    
    0 讨论(0)
  • 2021-01-06 14:13

    You need to use SelectedIndexChanged handler to show selected value:

    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;
    }
    
    0 讨论(0)
  • 2021-01-06 14:24

    this happens to me once then i code like this... but i didnt use the onLoad attribute, tell me if this works,

     <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>
    
    0 讨论(0)
  • 2021-01-06 14:25

    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

    0 讨论(0)
提交回复
热议问题