简体   繁体   中英

Passing a databound int in a gridview to a function via an on click event in the grid

Basically I'm trying to pass this int to a function from a link button in a grid view

here is what I'm doing atm but I am receiving an error.. I've been stuck on this all morning..

Thanks for any help.

Here is the code.

<asp:LinkButton runat="server" id="lnkAttachment" 
   ToolTip="<%$ Resources:DocumentsContent, Viewdocument%>" 
   Visible='<%# (int)DataBinder.Eval(Container.DataItem, "DOCUMENT_ID") > 0 %>' 
   OnClientClick="aspnetForm.target ='_blank'; setDownloadDoc();" 
   OnClick='lnkAttachment_Click(<%#(int)DataBinder.Eval(Container.DataItem, "DOCUMENT_ID")%>)'>

  <asp:Image ID="imgSelect0" runat="server"  src="../Images/IconEdit.gif" border="0" alt="<%$ Resources:DocumentsContent, Select%>" />

</asp:LinkButton>

如果您是为Web开发的,则可以使用查询字符串或会话作为替代。

Gawd webforms sucks.

Try using the CommandArgument attribute of the LinkButton. Look up how to use it.

You're not meant to actually call a method in the OnClick attribute, ie lnkAttachment_Click(stuff). You're meant to specify the name of the event handler and that's all, so lnkAttachment_Click.

Actually, screw it. Just use a normal <a> link and set all your own attributes. Use a data-something html5 attribute to store your integer and have the postback called from a client click handler. Avoid WebForms controls wherever you can, because it just makes good, modern web development so much harder.

Like David said, try adding commandArgument to your Link:

 CommandArgument='<%# Container.DataItemIndex %>' 

Then in your code behind, get the value like this maybe?

string Value = GridView2.Rows[Convert.ToInt32(((LinkButton)sender).CommandArgument.ToString())].Cells[0].Text;

I then used ResponseRedirects & Query strings. to pass the value around.

Hope this can help.

Since it is a asp control inside a GridView you need to set a Command to the LinkButton. You can not use the OnClick property.

<asp:LinkButton runat="server" id="lnkAttachment" 
   ToolTip="<%$ Resources:DocumentsContent, Viewdocument%>" 
   Visible='<%# (int)DataBinder.Eval(Container.DataItem, "DOCUMENT_ID") > 0 %>' 
   OnClientClick="aspnetForm.target ='_blank'; setDownloadDoc();" 
   CommandName="lnkAttachmentClick"
   CommandValue='<%#(int)DataBinder.Eval(Container.DataItem, "DOCUMENT_ID")%>'>
  <asp:Image ID="imgSelect0" runat="server"  src="../Images/IconEdit.gif" border="0" alt="<%$ Resources:DocumentsContent, Select%>" />
</asp:LinkButton>

The code behind would be like:

private void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
    switch (e.CommandName) {
        case "lnkAttachmentClick":
            int document_id = (int)e.CommandArgument;
            // Write your code here
            break;
}

Finally, you need to add the onrowcommand property in your GridView with the name of the function.

<asp:GridView ID="GridView1" onrowcommand="GridView1_RowCommand">
</asp:GridView>

You can view here a simple tutorial no how to use the RowCommand event in a GridView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM