Question

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:

GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;

Label category = (Label)clickedRow.FindControl("lbl_category");

String selectedCategory = category.Text.ToString();

string query = 
("SELECT * FROM main WHERE category='" + selectedCategory + "' ORDER BY ID ASC");

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 :)

EDIT - This is the gridview binding code:

<asp:TemplateField HeaderText="Category" ItemStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lbl_category" Text='<%# Bind("category")%>' runat="server" style="display:none;"></asp:Label>
<asp:LinkButton ID="lbl_linkCategory" Text='<%# Bind("category")%>' runat="server" OnClick="linkCategory" CommandArgument='<%# Bind("category")%>' />
</ItemTemplate>
</asp:TemplateField>
Was it helpful?

Solution 2

Using if (!IsPostBack) in Page_Load seemed to have solved the problem :)

OTHER TIPS

When you click on a category this changes the controls underneath. The control numbers changes. You reference that number and it is wrong, it needs to be constant and an ID of the table "main". You will have to find a way to assign the corresponding number to selectedCategory properly. You could do that by adding an attribute to the category-label, or check on what has been clicked. Like Finance => selectedCategory = 2, Health = 3, Family = 4 etc. Hopes it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top