【问题标题】:Getting the right row in a GridView在 GridView 中获取正确的行
【发布时间】:2010-11-12 08:58:56
【问题描述】:

我有一个 GridView,其中一个单元格包含一个带有 CalendarExtender 的 TextBox。另一个单元格包含一个触发 CalendarExtender 的按钮。选择日期后,会在客户端触发 checkDate 函数,最后我想触发按钮的服务器端事件。我唯一的问题是如何确定用户点击了哪一行,这样我就可以从 javascript 触发右键事件?

这是我的 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>

【问题讨论】:

    标签: javascript asp.net gridview calendarextender


    【解决方案1】:

    我不明白为什么你必须从 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();
    

    【讨论】:

    • 当一个 CalendarExtender 被一个按钮触发时,该按钮的服务器端 button_click 事件不会被触发。这就是为什么我必须在完成后从 javascript 触发它。在您的回答中,我仍然不知道用户在哪一行单击了按钮(显示在每一行上),我仍然需要它从该行中提取数据以用于我的事件背后的代码。
    • 能发一下gridView的源码吗? (只是“HTML”)
    • 好的,我已经添加了我的 GridView 的简化版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2014-06-28
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    相关资源
    最近更新 更多