1

I am making QueryString parameter ,so I have two .aspx forms. One form Default.aspx contains three fields SId, FirstName, LastName. when I submit the values, those values are saved in database.Now I want show this data on grid for particular SId on default2.aspx. Grid takes data through the SqlDataSource.SO I want to call the SqlDataSource on button click and grid should show data for particular SId on default2.aspx.

Default.aspx :

<body>
  <form id="form1" runat="server">
  <div>

  </div>
<table cellpadding="2" cellspacing="5">
    <tr>
        <td>
            <asp:Label ID="lblId" runat="server" Text="SId"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="tbid" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblname" runat="server" Text="FirstName"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="tbfirstname" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lbllastname" runat="server" Text="LastName"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="tblastname" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button ID="btnsubmit" runat="server" Text="Submit" 
                onclick="btnsubmit_Click1" Width="102px" />
        </td>
    </tr>
</table>
<br />

</form>
</body>

Default2.aspx :

<body>
<form id="form1" runat="server">
<div>

    <table cellpadding="2" cellspacing="5">
        <tr>
            <td>
                SId</td>
            <td>
                <asp:TextBox ID="tbId" runat="server"></asp:TextBox>
            </td>
        </tr>

        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="btnSearch" runat="server" Text="Search" 
                    onclick="btnSearch_Click" />
            </td>
        </tr>

    </table>  
</div>

<br />

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    AutoGenerateSelectButton="True" 
    DataSourceID="SqlDataSource1" EnableModelValidation="True" 
    onrowcommand="GridView1_RowCommand">
<Columns>

        <asp:TemplateField HeaderText="SId">
          <ItemTemplate>
           <asp:Label ID="lblSId" runat="server" Text='<%# Bind("SId") %>'></asp:Label>
           </ItemTemplate>

        </asp:TemplateField>

        <asp:TemplateField HeaderText="FirstName">
           <ItemTemplate>
          <asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FirstName")%>'>

          </asp:Label>
          </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="LastName">
           <ItemTemplate>
  <asp:Label ID="lblLastName" runat="server" Text='<%# Bind("LastName")%>'></asp:Label>
           </ItemTemplate>
        </asp:TemplateField>

</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:testConnectionString %>"  
    SelectCommand="SELECT * FROM [Student] where SId = @tbId">

<SelectParameters>
        <asp:QueryStringParameter Name="SId" QueryStringField="SId" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>

I want to call the SqlDataSource1 on button click like..

Default2.aspx.cs :

 protected void btnSearch_Click(object sender, EventArgs e)
 {     
    string SId = tbId.Text;
    SqlDataSource1.DataBind();

 }

1 Answer 1

3

You don't need the button click. The SqlDataSource executes on every post back. The search button is going to issue a post back. But you are going to need to change a few things. First you need to get your parameters right, so remove this one:

<asp:QueryStringParameter Name="SId" QueryStringField="SId" />

and put this one in its place:

<asp:ControlParameter ControlID="tbId" PropertyName="Text" Name="tbId" />

and now remove all of this code:

protected void btnSearch_Click(object sender, EventArgs e)
{     
    string SId = tbId.Text;
    SqlDataSource1.DataBind();
}

and now the SqlDataSource is tied directly to the control value, so when the user puts in a new control value and clicks the button it will be applied and new data retrieved without any of your interaction.

Sign up to request clarification or add additional context in comments.

1 Comment

@user2034832, fantastic, I'm glad I could be of assistance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.