代码很简单,实现的功能是,如果点击Edit (自带的),出现的Dropdownlist的初始选择值等于点击Edit以前的值public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Label mytext = (Label)(GridView1.Rows[e.NewEditIndex].FindControl("Label1"));
this.Label2.Text = mytext.Text; DropDownList mylist = (DropDownList)(GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1")); mylist.ClearSelection(); (运行的时候报错为:Object reference not set to an instance of an object.) mylist.Items.FindByValue(mytext.Text).Selected = true; }
}请问应该怎么修改啊!!!!!!急,想了几天了
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Label mytext = (Label)(GridView1.Rows[e.NewEditIndex].FindControl("Label1"));
this.Label2.Text = mytext.Text; DropDownList mylist = (DropDownList)(GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1")); mylist.ClearSelection(); (运行的时候报错为:Object reference not set to an instance of an object.) mylist.Items.FindByValue(mytext.Text).Selected = true; }
}请问应该怎么修改啊!!!!!!急,想了几天了
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedIndex='<%# GetSelectedTitle(Eval("Cat")) %>' (绑定)
DataSource='<%# Cat %>' >
<asp:ListItem>Mr.</asp:ListItem>
<asp:ListItem>Mrs.</asp:ListItem>
<asp:ListItem>Dr.</asp:ListItem>
<asp:ListItem>Ms.</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
protected string[] Cat
{
get { return new string[] { "Mr.", "Dr.", "Ms.", "Mrs." }; }
} protected int GetSelectedTitle(object title)
{
return Array.IndexOf(Cat, title.ToString());
}
{
GridView1.EditIndex = e.NewEditIndex;
DropDownList mylist= ((DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1")).SelectedValue.Trim();
if(mylist!=null)
{}
}
在protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
if ((DropDownList)e.Row.FindControl("DropDownList1") != null)
{
((DropDownList)e.Row.FindControl("DropDownList1")).SelectedValue = ""; }
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; DropDownList mylist = ((DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1")).SelectedValue.Trim(); if (mylist != null) (这里虽然不报错,但是因为mylist==null,所以会跳过这里的内容,无法得到想要的结果)
{
Label mytext = (Label)(GridView1.Rows[e.NewEditIndex].FindControl("Label1")); this.Label2.Text = mytext.Text; mylist.ClearSelection(); mylist.Items.FindByValue(mytext.Text).Selected = true;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
if ((DropDownList)e.Row.FindControl("DropDownList1") != null)
{
((DropDownList)e.Row.FindControl("DropDownList1")).SelectedValue = "";
}
}
}
如果放入ItemTemplate,那么就没法在Edit的时候实现我想要的功能了
protected void GvQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == GvQuestion.EditIndex)
{
DropDownList ddl = e.Row.FindControl("ddl_name") as DropDownList;
if (ddl != null)
{
String name = (e.Row.DataItem as DataRowView).Row.ItemArray[1].ToString();
for (int i = 0; i < ddl.Items.Count; i++)
{
if (ddl.Items[i].Text.Trim() == name.Trim())
{
ddl.SelectedIndex = i;
break;
}
}
}
}
}
protected void GvQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == GvQuestion.EditIndex)
{
DropDownList ddl = e.Row.FindControl("ddl_name") as DropDownList;
if (ddl != null)
{
String name = (e.Row.DataItem as DataRowView).Row.ItemArray[1].ToString();
for (int i = 0; i < ddl.Items.Count; i++)
{
if (ddl.Items[i].Text.Trim() == name.Trim())
{
ddl.SelectedIndex = i;
break;
}
}
}
}
}ding首先判断是否为空!
2 using System.Data;
3 using System.Data.SqlClient;
4 using System.Configuration;
5 using System.Collections;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Web.UI.HtmlControls;
12
13 public partial class Demo13 : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (Page.IsPostBack == false)
18 {
19 BindData();
20 }
21 }
22
23 public void BindData()
24 {
25 string strSql = "select UserID,C_Name,E_Name,QQ,Blood_Type from Demo_User ";
26 DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, CommandType.Text, strSql, null).Tables[0];
27
28 GridView.DataSource = dt;
29 GridView.DataKeyNames = new string[] { "UserID" };//主键
30 GridView.DataBind();
31 }
32
33 protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
34 {
35 GridView.PageIndex = e.NewPageIndex;
36 BindData();
37 }
38
39 protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
40 {
41 if (((DropDownList)e.Row.FindControl("DropDownList")) != null)
42 {
43 DropDownList DropDownList = (DropDownList)e.Row.FindControl("DropDownList");
44 DropDownList.SelectedValue = ((HiddenField)e.Row.FindControl("HiddenField")).Value;
45 }
46 }
47
48 protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
49 {
50 DropDownList DropDownList = sender as DropDownList;
51 int index = (DropDownList.NamingContainer as GridViewRow).RowIndex;
52 string UserID = GridView.DataKeys[index].Value.ToString();
53
54 string strSql = "Update Demo_User set Blood_Type=@Blood_Type where UserID=@UserID ";
55 SqlParameter[] para = {
56 new SqlParameter("@Blood_Type", DropDownList.SelectedValue.ToString()),
57 new SqlParameter("@UserID", UserID),
58 };
59 SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.Text, strSql, para);
60 BindData();
61 }
62 }
1 <table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1" width="99%" >
2 <tr>
3 <th colspan="2">
4 GridView演示</th>
5 </tr>
6 <tr>
7 <td colspan="2" style="width: 100%;" >
8 <asp:GridView ID="GridView" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GridView_PageIndexChanging" PageSize="12" OnRowDataBound="GridView_RowDataBound" >
9 <Columns>
10 <asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True" />
11 <asp:BoundField DataField="C_Name" HeaderText="中文名字" ReadOnly="True" />
12 <asp:BoundField DataField="E_Name" HeaderText="英文名字" ReadOnly="True" />
13 <asp:BoundField DataField="QQ" HeaderText="QQ" />
14 <asp:TemplateField HeaderText="学生血型">
15 <ItemTemplate>
16 <asp:DropDownList ID="DropDownList" runat="server" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" AutoPostBack="True">
17 <asp:ListItem Value="1">A</asp:ListItem>
18 <asp:ListItem Value="2">B</asp:ListItem>
19 <asp:ListItem Value="3">AB</asp:ListItem>
20 <asp:ListItem Value="4">O</asp:ListItem>
21 </asp:DropDownList>
22 <asp:HiddenField ID="HiddenField" runat="server" Value='<%# Eval("Blood_Type") %>'/>
23 </ItemTemplate>
24 </asp:TemplateField>
25 </Columns>
26 <RowStyle HorizontalAlign="Center" />
27 <PagerStyle HorizontalAlign="Right" />
28 </asp:GridView>
29 </td>
30 </tr>
31 </table>
在模板列中放入两个控件,一个是DropDownList,另一个是HiddenField,让HiddenField绑定选择的值,然后在事件DataBound时用,先判断GridView1.CurrentMode==DetailsViewMode.Edit时,查找出DropDownList,让它的值等于你隐藏控件HiddenField中的值就可以了。