获取 GridView 中的正确行
我有一个 GridView,其中一个单元格包含一个带有 CalendarExtender 的 TextBox。另一个单元格包含一个触发 CalendarExtender 的按钮。选择日期后,在客户端触发 checkDate 函数,最后我想触发按钮的服务器端事件。我唯一的问题是如何确定用户单击了哪一行,以便我可以从 javascript 触发右键事件?
这是我的网格视图:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical"
OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Movie ID">
<ItemTemplate>
<asp:Label runat="server" ID="lblMovieId" Text='<%#Eval("MovieId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie Name">
<ItemTemplate>
<%#Eval("MovieName") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Return Date">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtRetDate" Text='<%# ((DateTime)Eval("ReturnDate")).ToShortDateString()%>'
BackColor="#EEEEEE" BorderStyle="None"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="btnUpdate"
TargetControlID="txtRetDate" Format="dd/MM/yyyy" OnClientDateSelectionChanged="checkDate" >
</asp:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" HeaderStyle-Width="135px">
<ItemStyle VerticalAlign="Top" />
<ItemTemplate>
<asp:Button runat="server" ID="btnUpdate" Text="Update" CommandName="Update" />
<asp:Button runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have a GridView where one of the cells holds a TextBox with a CalendarExtender attached to it. Another cell holds a button that triggers the CalendarExtender. After a date is selected, a checkDate function is triggered on the client side where at the end of it I want to trigger the server side event of the button. My only problem is how do I figure out which row the user clicked, so I can trigger the event of the right button from javascript?
Here's my GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical"
OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Movie ID">
<ItemTemplate>
<asp:Label runat="server" ID="lblMovieId" Text='<%#Eval("MovieId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie Name">
<ItemTemplate>
<%#Eval("MovieName") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Return Date">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtRetDate" Text='<%# ((DateTime)Eval("ReturnDate")).ToShortDateString()%>'
BackColor="#EEEEEE" BorderStyle="None"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="btnUpdate"
TargetControlID="txtRetDate" Format="dd/MM/yyyy" OnClientDateSelectionChanged="checkDate" >
</asp:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" HeaderStyle-Width="135px">
<ItemStyle VerticalAlign="Top" />
<ItemTemplate>
<asp:Button runat="server" ID="btnUpdate" Text="Update" CommandName="Update" />
<asp:Button runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白为什么你必须从 JS 调用服务器端的 Button_Click 事件处理程序...
如果您需要从 javascript 在服务器端调用“某些内容”作为在 javascript 函数中执行的最后一个操作,您可以使用 JQuery/Javascript 来调用该函数。您应该重构您的代码,将服务器端按钮单击的代码放入一个方法中,您可以从服务器端 Button_Click 和 javascript 调用该方法...
在服务器端,我假设您有 button_click 事件。您必须重构如下所示:
在您的页面上,您必须添加脚本管理器:
I don't understand why you have to call Button_Click event handler on the server side from JS...
If you need call "something" on the server side from javascript as last operation to do within a javascript function you could use JQuery/Javascript to call that function. You should refactor your code putting the code of the button click on the server side into a method that you can call both from che server side Button_Click and from javascript...
On the server side I assume you the button_click event.You have to refactor it as showed below:
On you page you have to add a script manager: