Updating asp:SqlDataSource Parameter via asp:LinkButton
I'll try to explain what I'm doing the best I can, but I'm pretty new to asp.net so be patient with me.
I have a SqlDataSource which returns a simple select statement based on the WHERE clause using @COURSE_ID
What I want to-do is every time any one of 2 (this will change as it's going to be generated) asp:LinkButtons are pressed, they will change the @COURSEID value which i'd like to associate with the specific button.
Buttons:
<asp:LinkButton ID="LinkButton2" runat="server" onclick="MenuUpdate_Click">Course1</asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="MenuUpdate_Click">Course2</asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connString %>" SelectCommand="SELECT Chapter.chapterName, Chapter.chapterID
FROM Chapter WHERE Chapter.courseID = @COURSE_ID ">
C# protected void MenuUpdate_Click(object sender, EventArgs e) {
Parameter p = SqlDataSource1.SelectParameters["COURSE_ID"];
SqlDataSource1.SelectParameters.Remove(p);
SqlDataSource1.SelectParameters.Add("COURSE_ID", THIS NEEDS TO BE VALUE ASSOCIATED TO BUTTON);
ListView1.DataBind();
UpdatePanel1.Update();
}
I开发者_运维问答f anyone has any suggestions that'd be great, I've been trying lots of different things all night with no success :(
Thanks
Try setting the linkbutton IDs to contain the course name (I dont think its possible for IDs to start with numbers, so give them IDs "crs" and then the course name). In the click event for the buttons, extract the ID of the sender and take the entire string after "crs". This is the course name of the linkbutton that was clicked.
Also if "COURSE_ID" is the only parameter of the SelectCommand, I would personally clear all the parameters first. It just ensures that you dont have any parameters from elsewhere. Thats just the way I work though - clear everything and reenter anything which shouldnt have been cleared and then enter anything extra.
Regards,
Rich
精彩评论