Tuesday, 15 May 2012

asp.net - How do I trigger a button click during page_load? -



asp.net - How do I trigger a button click during page_load? -

i have few buttons created while loop , stored within placeholder, when created have commandarguments equal unique fields single column of database. when clicked, button's click procedure retrieves info database using button's commandargument determine info display.

my problem when page loads, want first of these buttons selected default , onclick event triggered. there way within if not page.ispostback, or have resort javascript/jquery?

here's code far:

<!doctype html> <%@ page language="vb" debug="true" clienttarget="uplevel" %> <%@ import namespace="system.data.oledb" %> <%@ import namespace="system.data" %> <script runat="server"> dim dbconnection oledbconnection dim dbcommand oledbcommand dim dbreader oledbdatareader dim sqlstring string sub page_load() seek dbconnection = new oledbconnection("provider=microsoft.ace.oledb.12.0;data source=" & server.mappath("mydatabase.accdb")) dbconnection.open() sqlstring = "select distinct type mytable" dbcommand = new oledbcommand(sqlstring, dbconnection) dbreader = dbcommand.executereader() while dbreader.read() dim mybutton button = new button() mybutton.id = dbreader("column2") mybutton.text = dbreader("column2") mybutton.commandargument = dbreader("column2") mybutton.enableviewstate = false addhandler mybutton.command, addressof myclicksub myplaceholder.controls.add(mybutton) end while dbreader.close() dbconnection.close() end seek end sub sub myclicksub(src object, args commandeventargs) seek dbconnection.open() sqlstring = "select * mytable column2 = '" & args.commandargument & "'" dbcommand.commandtext = sqlstring dbreader = dbcommand.executereader() myrepeater.datasource = dbreader myrepeater.databind() dbreader.close() dbconnection.close() end seek end sub </script> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>my page</title> </head> <body> <form runat="server"> <div> <asp:placeholder id="myplaceholder" runat="server" /> <br /> <asp:repeater id="myrepeater" runat="server"> <headertemplate> <table> <tr> <th scope="col">id</th> <th scope="col">foo</th> <th scope="col">bar</th> </tr> </headertemplate> <itemtemplate> <tr> <td><%# eval("column1")%></td> <td><%# eval("column3")%></td> <td><%# eval("column4")%></td> </tr> </itemtemplate> <footertemplate> </table> </footertemplate> </asp:repeater> </div> </form> </body> </html>

i want first of these buttons selected default

technically want first record display default. separate concern clicking of button. separate own function:

sub bindrepeater(byval column2 string) seek dbconnection.open() sqlstring = "select * mytable column2 = '" & column2 & "'" dbcommand.commandtext = sqlstring dbreader = dbcommand.executereader() myrepeater.datasource = dbreader myrepeater.databind() dbreader.close() dbconnection.close() end seek end sub

then in click handler:

sub myclicksub(src object, args commandeventargs) bindrepeater(args.commandargument) end sub

now can phone call same function page_load:

if not page.ispostback bindrepeater(somevalue) end if

(where somevalue you. imagine it's first record in info source?)

also, , this important, info access code wide open sql injection attacks. you'll really want utilize parameterized queries instead.

asp.net

No comments:

Post a Comment