asp.net - How can i return value from Gridview-RowDataBound to use it in link. C# -
im trying delete gridview row on button click. want save clicked row's id variable. , use variable in hyperlink.
here rowdatabound code
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { e.row.attributes.add("onmouseover", "this.style.cursor='pointer';this.style.backgroundcolor='yellow'"); e.row.rowindex.tostring())); string id = databinder.eval(e.row.dataitem, "productionorderid").tostring(); // somthing // return id ; } }
here hyperlink need id of selected row
<asp:hyperlink runat="server" navigateurl="~/producter/delete?id= id" id="hyperlink1"> delete</asp:hyperlink>
firstly: add reference jquery head section of master page
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
secondly: add productionorderid datarow attribute (as shown below), way accessible on client side via jquery
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { e.row.attributes.add("onmouseover", "this.style.cursor='pointer';this.style.backgroundcolor='yellow'"); e.row.rowindex.tostring(); string id = databinder.eval(e.row.dataitem, "productionorderid").tostring(); //save productionorderid datarow attribute e.row.attributes.add("rowid", id); } }
thirdly:
in body of .aspx file add following script tag. every time row clicked modifies 'delete' link id of row delete. have included link sake of clarity , completeness.
<a href='<%=resolveurl("~/producter/delete?id=" ) %>' id="hyperlink1">delete</a> <script language="javascript"> //every time row clicked script perform following actions: $("tr").click(function () { var clicked = $(this); //get row id cliked row var rowid = clicked.attr("rowid"); //get value of href attribute link id 'hyperlink1' var link = $("#hyperlink1").attr("href"); //remove appended values var linktokens = link.split("="); linktokens[1] = ""; link = linktokens.join("="); //append current row id link link = link + rowid; //set href attribute of link new value $("#hyperlink1").attr("href", link); }); </script>
should need further assistance please not hesitate let me know.
disclaimer: best use cdn deliver js files since highly have been cached user browser.
as requested, here how put jquery library 2.0 in content folder:
- make backup of working solution.
- click on this link right mouse button , choose save as.
- save content folder on disk.
- from visual studio choose 'add existing item'
- browse content folder
- select jquery file
- click add.
- now drag jquery file in header section of page.
- delete old tag (link ajax.googleapis.com)