Gridview OnSelectedIndexChanged="ddlrefresh_SelectedIndexChanged"> 的事件永远不触发?

本文探讨了在ASP.NET中如何正确地为GridView内的DropDownList设置事件处理程序,特别是通过RowDataBound事件来实现自动回发及事件绑定的方法,并提供了解决方案。


   


-------------------------------------------------------------------------------------------------------------------------------------

上面这样写dropdownlist 的事件永远不触发?


但是当代码换成

-------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------

就会执行。
为什么第一种方法不执行呢?

-------------------------------------------------------------------------------------------------------------------------------------

解决方法:

 

 

将事件代码加入到
RowCeated

就行了。

 

aspx.代码如下<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="选课表.aspx.cs" Inherits="学生选课系统0.WebForm6" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <style type="text/css"> body { background-image:url(../assets/images/5.jpg); background-size:cover; } </style> <form id="form1" runat="server"> <div> </div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="serial_no" DataSourceID="SqlDataSource1" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="serial_no" HeaderText="serial_no" ReadOnly="True" SortExpression="serial_no" /> <asp:BoundField DataField="student_id" HeaderText="student_id" SortExpression="student_id" /> <asp:BoundField DataField="course_id" HeaderText="course_id" SortExpression="course_id" /> <asp:BoundField DataField="course_name" HeaderText="course_name" SortExpression="course_name" /> <asp:BoundField DataField="select_time" HeaderText="select_time" SortExpression="select_time" /> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowDeleteButton="True" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:学生选课系统ConnectionString %>" SelectCommand="SELECT * FROM [selection]"></asp:SqlDataSource> <asp:Button ID="Button1" runat="server" PostBackUrl="~/选课信息添加.aspx" style="margin-bottom: 0px" Text="添加" /> </form> </body> </html> .cs代码如下using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace 学生选课系统0 { public partial class WebForm6 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string serial_no = GridView1.DataKeys[e.RowIndex].Value.ToString(); string student_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString(); string course_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString(); string course_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString(); string sqlStr = "update selection set student_id='" + student_id + "',course_id=" + course_id + " , course_name ='" + course_name + "', select_time = Getdate() where serial_no= " + serial_no; String str = "Server=10.13.111.79;User Id=cc;Pwd=Lkp007958;DataBase=学生选课系统"; SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand cmd = new SqlCommand(sqlStr, conn); SqlDataSource1.UpdateCommand = sqlStr; int num = cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); GridView1.EditIndex = -1; GridView1.DataBind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string serial_no = GridView1.DataKeys[e.RowIndex].Value.ToString(); string sqlStr = "DELETE FROM selection WHERE serial_no = @serial_no"; string str = "Server=10.13.111.79;User Id=cc;Pwd=Lkp007958;DataBase=学生选课系统"; SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand cmd = new SqlCommand(sqlStr, conn); cmd.Parameters.AddWithValue("@serial_no", serial_no); int num = cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); GridView1.EditIndex = -1; GridView1.DataBind(); Response.Redirect(Request.Url.PathAndQuery); } } }如何添加一个按student_id查询功能
最新发布
06-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rjzou2006

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值