asp.net - When i click button in gridview it does't work -
i beginner of asp.net, making l small project having big problems, :) ... right have situation, used gridview
retrieve data access database , done that, added buttons in grid view. when click on button following error comes
server error in '/e shop' application. invalid postback or callback argument. event validation enabled using in configuration or <%@ page enableeventvalidation="true" %> in page. security purposes, feature verifies arguments postback or callback events originate server control rendered them. if data valid , expected, use clientscriptmanager.registerforeventvalidation method in order register postback or callback data validation.
html
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" width= "100%" cellpadding="3"> <columns> <asp:templatefield> <itemtemplate> <table class="style17" width="100%" border=" 0"> <tr> <td height="100%" width="25%"> <asp:image id="image6" runat="server" height="144px" imageurl='<%# "data:image/jpg;base64, " + convert.tobase64string((byte[]) eval("picture")) %>' width="158px" bordercolor="black" borderstyle="inset" borderwidth="1px" /> </td> <td align="center" height="100%" width="75%"> <table align="right" class="style17"> <tr> <td align="left"> <asp:label id="label7" runat="server" style="font-size: 15pt; color: #0000ff" text='<%# bind("title") %>'></asp:label> </td> <td> <asp:label id="label6" runat="server" text='<%# bind("brand") %>' cssclass="style18" font-size="15pt"></asp:label> </td> <td align="right"> <asp:imagebutton id="imagebutton1" runat="server" height="51px" imageurl="~/images/orange_addtocart-trans.png" width="159px" onclick="imagebutton1_click" /> </td> </tr> <tr> <td align="left"> <strong>rs:</strong><asp:label id="label2" runat="server" text='<%# bind("price") %>' cssclass="style18" font-size="15pt"></asp:label> </td> <td> <asp:label id="label3" runat="server" text='<%# bind("color") %>' cssclass="style18" font-size="15pt"></asp:label> </td> <td align="right"> <asp:imagebutton id="imagebutton2" runat="server" height="51px" imageurl="~/images/orange_addtocart-trans.png" width="159px" onclick="imagebutton2_click" /> </td> </tr> <tr> <td align="left"> <asp:label id="label4" runat="server" text='<%# bind("condition") %>' cssclass="style18" font-size="15pt"></asp:label> </td> <td> <asp:label id="label5" runat="server" text='<%# bind("material") %>' cssclass="style18" font-size="15pt"></asp:label> </td> <td> </td> </tr> </table> </td> </tr> </table> </itemtemplate> </asp:templatefield> </columns> <emptydatatemplate> <table class="style17"> <tr> <td align="left" height="100%" width="30%"> <asp:image id="image5" runat="server" height="144px" imageurl='<%# "data:image/jpg;base64, " + convert.tobase64string((byte[]) eval("picture")) %>' width="193px" /> </td> <td> </td> </tr> </table> </emptydatatemplate> </asp:gridview>
c#
public partial class shirts : system.web.ui.page { protected void page_load(object sender, eventargs e) { oledbconnection con = new oledbconnection("provider=microsoft.jet.oledb.4.0;data source =" + server.mappath("~\\app_data\\products.mdb")); con.open(); oledbcommand cmd = new oledbcommand(); cmd.commandtext = "select * shirts"; cmd.connection = con; oledbdataadapter adaptor = new oledbdataadapter(cmd); dataset ds = new dataset(); adaptor.fill(ds); gridview1.datasource = ds; gridview1.databind(); con.close(); } protected void imagebutton1_click(object sender, imageclickeventargs e) { // session["title"] = "label7.text"; } protected void imagebutton2_click(object sender, imageclickeventargs e) { // response.redirect("cart.aspx"); } protected void button1_click(object sender, eventargs e) { // response.redirect("cart.aspx"); } protected void button2_click(object sender, eventargs e) { response.redirect("cart.aspx"); } }
please me make buttons work in advance
you can't create event handler controls present in gridview
control this, because not static , repeat each item coming datasource gridview.
when button clicked in gridview, rowcommand event raised, need write logic in event identifying control, this:-
to identify each control individually in gridview rowcommand event, need add commandname
& commandargument
properties control:-
<asp:imagebutton id="imagebutton1" runat="server" height="51px" imageurl="~/images/orange_addtocart-trans.png" width="159px" oncommand="foo" commandargument="<%# ((gridviewrow) container).rowindex %>"/>
here, commandname used identify control raised event , commandargument used identify current row.
finally, can read them in code behind this:-
protected void gridview1_rowcommand(object sender, gridviewcommandeventargs e) { if (e.commandname == "foo") { int index = convert.toint32(e.commandargument); gridviewrow row = gridview1.rows[index]; // add logic here } }
Comments
Post a Comment