GridView控件中模板列DropDownList的SelectedIndexChanged事件不响应
GridView控件中模板列DropDownList的SelectedIndexChanged事件(方法一)protectedvoidDropDownList_Selec...
GridView控件中模板列DropDownList的SelectedIndexChanged事件(方法一)
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DropDownList = sender as DropDownList;
int index = (DropDownList.NamingContainer as GridViewRow).RowIndex;
string UserID = GridView.DataKeys[index].Value.ToString();
string strSql = "Update Demo_User set Blood_Type=@Blood_Type where UserID=@UserID ";
SqlParameter[] para = {
new SqlParameter("@Blood_Type", DropDownList.SelectedValue.ToString()),
new SqlParameter("@UserID", UserID),
};
SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.Text, strSql, para);
BindData();
}
}
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>
我解决了,是在pageload中没有写入
if(!IsPostBack){} 展开
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DropDownList = sender as DropDownList;
int index = (DropDownList.NamingContainer as GridViewRow).RowIndex;
string UserID = GridView.DataKeys[index].Value.ToString();
string strSql = "Update Demo_User set Blood_Type=@Blood_Type where UserID=@UserID ";
SqlParameter[] para = {
new SqlParameter("@Blood_Type", DropDownList.SelectedValue.ToString()),
new SqlParameter("@UserID", UserID),
};
SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.Text, strSql, para);
BindData();
}
}
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>
我解决了,是在pageload中没有写入
if(!IsPostBack){} 展开
3个回答
展开全部
断点能跟进去吗,如果哪都没问题,也有可能是DropDownList的AutoPostBack属性没有设置成true。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
生成网页后你看一下源文件dropdownlist控件的ID值会加上gridview的ID值ID对不上,所以后台那个indexchanged事件不会执行的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DropDownList DropDownList = sender as DropDownList;
我觉得这一句应该有毛病,应该 DropDownList ddl= sender as DropDownList;
不能直接用DropDownList
我觉得这一句应该有毛病,应该 DropDownList ddl= sender as DropDownList;
不能直接用DropDownList
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询