简体   繁体   中英

Dropdown list value not displaying

hi i have gridview that when a row is selected, it populates into textboxes that are used to populate the gridview itself. the last field is a dropdownlist and its not displaying when the gridview is clicked. i set a breakpoint and see that its stuck on the first - 0 index. i dont know why it isnt moving forward... here is the code:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    ....

    if (DropDownListCurrency.Items.FindByValue(row.Cells[7].Text.ToString().Trim) != null)
    {
        DropDownListCurrency.SelectedValue = row.Cells[7].Text.ToString().Trim();
    }
    ....
}



<asp:DropDownList ID="DropDownListCurrency" runat="server" 
        CausesValidation="True"     
        DataSourceID="CurrencyDropDownListDataSource" 
        DataTextField="Currency" DataValueField="Currency_ID"
        AppendDataBoundItems="True">
    <asp:ListItem Value="0" Text="&lt;Select&gt;" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>

why you want take the value from textbox. Will better use DataKeyNames like this inside that event

GridViewRow row = GridView.SelectedRow;

int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);

this work if you have only one value in DataKeyName if you look not there a index if you want have more than one value use this

int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Values["FirstValue"]);

string name = Convert.ToString(GridView.DataKeys[row.RowIndex].Values["SecondValue"]);

DropDownList是否包含您想要显示的单词?

You need to set the AutoPostBack property to True :

it should be :

<asp:DropDownList ID="DropDownListCurrency" AutoPostBack="True" runat="server" 
        CausesValidation="True"     
        DataSourceID="CurrencyDropDownListDataSource" 
        DataTextField="Currency" DataValueField="Currency_ID"
        AppendDataBoundItems="True">
    <asp:ListItem Value="0" Text="&lt;Select&gt;" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>

made a simple error...

called the wrong field was supposed to be value (Currency_ID) and not the text (Currency)

DropDownListCurrency.SelectedValue = GridView1.DataKeys[row.RowIndex].Values["Currency_ID"].ToString().Trim();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM