在gridview中编辑一行时,如何显示Drodownlist的默认selectedvalue? [英] How to display the default selectedvalue of a Drodownlist when edit a row in a gridview ?

查看:80
本文介绍了在gridview中编辑一行时,如何显示Drodownlist的默认selectedvalue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,其中一列已转换为TemplateField,如下所示:

I have a gridview, in which one column has been transoformed to TemplateField that likes this:

<asp:TemplateField HeaderText="name" SortExpression="name">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox_Name" runat="server" Text='<%# Bind("name") %>' </asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label_Name" runat="server" Text='<%# Bind("name") %>'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField>
<asp:TemplateField HeaderText="id" SortExpression="id">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox_ID" runat="server" Text='<%# Bind("id") %>' </asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label_ID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="sex" SortExpression="sex">
    <ItemTemplate>
         <asp:Label ID="Label_Sex" runat="server" Text='<%# Bind("sex") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
         <asp:DropDownList ID="DropDownList_Sex" runat="server" >
             <asp:ListItem>Male</asp:ListItem>
             <asp:ListItem>Female</asp:ListItem>
             </asp:DropDownList>
    </EditItemTemplate>
 </asp:TemplateField>




如果我从数据库加载记录,例如:




If I load a record from DB, for example :

ID      Name             Sex                    
098     Mary Loen        Sorry, It is a secret.



当我在gridview的更新事件中编辑此记录时,显示的下拉列表的selected值不是对不起,这是一个秘密."记录,而是第一项男性".

C#代码为:



When I edit this record in the gridview''s updating event, the displayed dropdownlist''s selectedvalue is not the record "Sorry, It is a secret.", but the first item - "Male".

The C# code is:

protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   DataTable dt = new DataTable();
   dt = (DataTable)Cache["dataTable"];
   dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
   DataRow dr = dt.Rows.Find(Session["ID"].ToString());

   TextBox tb = (TextBox)GridView.Rows[e.RowIndex].FindControl("TextBox_Name");
   dr["name"] = tb.Text.Trim();

   drdList = (DropDownList)(GridView.Rows[e.RowIndex].Cells[1].FindControl("DropDownList_Sex"));
   dr["sex"] = drdList.SelectedValue;

   GridView.EditIndex = -1;
   FillData2Grid();
}
	
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
   GridView.EditIndex = e.NewEditIndex;
   FillData2Grid();
   TextBox tb = (TextBox)GridView.Rows[e.NewEditIndex].FindControl("TextBox_ID");
   Session["ID"] = tb.Text;
}



如何使下拉列表显示原始的选定值?



How could I make the dropdownlist show the original selectedvalue ?

推荐答案

在Page_Load中添加到下拉列表抱歉,这是一个秘密,很抱歉,您从数据库中加载并选择了它
In Page_Load add to dropdownlist Sorry it''s a secret, what you load from a database and select it




如果可以的话,尝试一下..

在页面加载事件中,找到下拉列表的索引位置.

女性的索引位置示例:
0 =-选择一个-"
1 =男"
2 =女性"

Hi,

Try this if could help..

In your page load event find the index position of your dropdown list.

Example index position of Female:
0 = "--Select One--"
1 = "Male"
2 = "Female"

this.ddlGender.SelectedIndex = 2;



上面的代码将显示您最初选择的值为 Female .

如果可以的话请投票...

问候



The above code will display your original selected value which is Female.

Please vote if could help...

Regards,


这篇关于在gridview中编辑一行时,如何显示Drodownlist的默认selectedvalue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆