Joseph Webber
Joseph Webber

Reputation: 2173

How do I trigger a button click during page_load?

I have a few buttons created with a while loop and stored inside a placeholder, which when created have CommandArguments equal to unique fields from a single column of my database.
When clicked, the button's click procedure retrieves data from the database using the button's CommandArgument to determine what data to display.

My problem is when the page loads, I want the first of these buttons to be selected by default and its onClick event to be triggered. Is there a way to do this inside an If Not Page.IsPostBack, or do I have to resort to JavaScript/jQuery?

Here's my code so far:

<!DOCTYPE HTML>
<%@ Page Language="VB" Debug="true" ClientTarget="uplevel" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.Data" %>

<script runat="server">
    Dim dbConnection As OleDbConnection
    Dim dbCommand As OleDbCommand
    Dim dbReader As OleDbDataReader
    Dim sqlString As String

    Sub Page_Load()
        Try
            dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" & Server.MapPath("MyDatabase.accdb"))
            dbConnection.Open()

            sqlString = "SELECT DISTINCT Type FROM MyTable"
            dbCommand = New OleDbCommand(sqlString, dbConnection)
            dbReader = dbCommand.ExecuteReader()

            While dbReader.Read()
                Dim myButton As 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()
        Finally
            dbConnection.Close()
        End Try
    End Sub

    Sub myClickSub(src As Object, args As CommandEventArgs)
        Try
            dbConnection.Open()
            sqlString = "SELECT * FROM MyTable WHERE Column2 = '" & args.CommandArgument & "'"
            dbCommand.CommandText = sqlString
            dbReader = dbCommand.ExecuteReader()

            myRepeater.DataSource = dbReader
            myRepeater.DataBind()

            dbReader.Close()
        Finally
            dbConnection.Close()
        End Try
    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>

Upvotes: 0

Views: 88

Answers (2)

David
David

Reputation: 218837

I want the first of these buttons to be selected by default

Technically you want the first record to display by default. This is a separate concern from the clicking of a button. So separate it into its own function:

Sub BindRepeater(ByVal column2 as String)
    Try
        dbConnection.Open()
        sqlString = "SELECT * FROM MyTable WHERE Column2 = '" & column2 & "'"
        dbCommand.CommandText = sqlString
        dbReader = dbCommand.ExecuteReader()

        myRepeater.DataSource = dbReader
        myRepeater.DataBind()

        dbReader.Close()
    Finally
        dbConnection.Close()
    End Try
End Sub

Then in the click handler:

Sub myClickSub(src As Object, args As CommandEventArgs)
    BindRepeater(args.CommandArgument)
End Sub

Now you can call that same function from Page_Load:

If Not Page.IsPostBack
    BindRepeater(someValue)
End If

(Where you get someValue is up to you. I imagine it's something from the first record in the data source?)


Also, and this is important, your data access code is wide open to SQL injection attacks. You'll really really want to use parameterized queries instead.

Upvotes: 1

Neeraj
Neeraj

Reputation: 4489

Hey add below code under the Page.IsPostBack event

btnSwitchToType1_Click(this, null);

Hope it helps

Upvotes: 0

Related Questions