开发者

Getting the right row in a 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>


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜