获取 GridView 中的正确行

发布于 2024-10-02 06:07:32 字数 2147 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

层林尽染 2024-10-09 06:07:32

我不明白为什么你必须从 JS 调用服务器端的 Button_Click 事件处理程序...
如果您需要从 javascript 在服务器端调用“某些内容”作为在 javascript 函数中执行的最后一个操作,您可以使用 JQuery/Javascript 来调用该函数。您应该重构您的代码,将服务器端按钮单击的代码放入一个方法中,您可以从服务器端 Button_Click 和 javascript 调用该方法...

在服务器端,我假设您有 button_click 事件。您必须重构如下所示:

Public void Button_Click(object sender,eventArgs e){

FuntionIWantCall();
}


//this is the code that was into the button click befeore refactoring:

private void FuntionIWantCall(){

//Do something on the server side
}


//this is the function you can call from javascript

[WebMethod]
public static void CalledFromJS(){
FuntionIWantCall();
}  

在您的页面上,您必须添加脚本管理器:

<asp:ScriptManager ID="ScriptManager1" runat="server"     EnablePageMethods="True"></asp:ScriptManager>


//at the end of your javascript function

PageMethods.CalledFromJS();

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:

Public void Button_Click(object sender,eventArgs e){

FuntionIWantCall();
}


//this is the code that was into the button click befeore refactoring:

private void FuntionIWantCall(){

//Do something on the server side
}


//this is the function you can call from javascript

[WebMethod]
public static void CalledFromJS(){
FuntionIWantCall();
}  

On you page you have to add a script manager:

<asp:ScriptManager ID="ScriptManager1" runat="server"     EnablePageMethods="True"></asp:ScriptManager>


//at the end of your javascript function

PageMethods.CalledFromJS();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文