[英]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 基本上,我试图将此int从网格视图中的链接按钮传递给函数
here is what I'm doing atm but I am receiving an error.. I've been stuck on this all morning.. 这是我正在做的atm,但是我收到一个错误..我整个上午都被困在里面。
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. Gawd webforms很烂。
Try using the CommandArgument attribute of the LinkButton. 尝试使用LinkButton的CommandArgument属性。 Look up how to use it.
查找如何使用它。
You're not meant to actually call a method in the OnClick attribute, ie lnkAttachment_Click(stuff). 您并不是要实际上在OnClick属性中调用方法,即lnkAttachment_Click(stuff)。 You're meant to specify the name of the event handler and that's all, so lnkAttachment_Click.
您只需要指定事件处理程序的名称,仅此而已,所以lnkAttachment_Click。
Actually, screw it. 实际上,拧紧它。 Just use a normal <a> link and set all your own attributes.
只需使用普通的<a>链接并设置所有自己的属性即可。 Use a data-something html5 attribute to store your integer and have the postback called from a client click handler.
使用data-something html5属性存储您的整数,并从客户端单击处理程序调用回发。 Avoid WebForms controls wherever you can, because it just makes good, modern web development so much harder.
尽可能避免使用WebForms控件,因为它会使良好的现代Web开发更加困难。
Like David said, try adding commandArgument to your Link: 就像David所说的那样,尝试将commandArgument添加到您的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. 然后,我使用了ResponseRedirects和Query字符串。 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. 由于它是GridView中的一个asp控件,因此需要将一个
Command
设置为LinkButton。 You can not use the OnClick
property. 您不能使用
OnClick
属性。
<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. 最后,您需要在GridView中添加带有函数名称的
onrowcommand
属性。
<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
. 您可以在此处查看一个简单的教程,其中没有有关如何在
GridView
使用RowCommand
事件的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.