locked
GridViewRow query linkbutton RRS feed

  • Question

  • User-1257946675 posted

    I am building a simple C# web app which will act like an online database for resources.

    In my table, I have a category and author column.

    When I click a category value in the table, the table will refresh showing only the categories that was selected.

    To do this I used the following code (front-end):

    <asp:TemplateField HeaderText="Author" ItemStyle-Width="15%">
    	<ItemTemplate>
    		<asp:LinkButton ID="lbl_linkAuthor" Text='<%# Bind("author")%>' runat="server" OnClick="linkAuthor" CommandArgument='<%# Bind("author")%>'  />
    	</ItemTemplate>
    </asp:TemplateField>

    And this is the code used in the back-end (c#) :

    protected void linkAuthor(object sender, EventArgs e)
    {
    	LinkButton btn = (LinkButton)(sender);
    	string selectedAuthor = btn.CommandArgument;
    
    	string query = ("SELECT * FROM main WHERE author='" + selectedAuthor + "' ORDER BY ID ASC");
    	open_con(query); //method which connects to a database
    	query_info.Text = ("Displaying content by " + selectedAuthor + ".");
    	reset_parameters();
    }

    This works fine for the first time I click a category/author. But after the table has refreshed, and select another category or author then the table shows the wrong record.

    How can I solve this?

    You can view the page here: Try clicking at category 'Health' and then 'Puvent, Kevin'. The outcome is different to the one that was expected. I think

    The question will probably make more sense once you see the page Smile

    Thank you for your help Smile

    Saturday, October 20, 2012 8:44 AM

Answers

  • User-179079940 posted

    try it write page load in which

    if(!page.ispostback)

    {

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 20, 2012 8:49 AM

All replies

  • User-179079940 posted

    try it write page load in which

    if(!page.ispostback)

    {

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 20, 2012 8:49 AM
  • User-1257946675 posted

    Thank you soo much! 

    using (!IsPostBack) helped Laughing

    Saturday, October 20, 2012 4:24 PM