Monday, March 26, 2012

Validating GridView Selection

I'm trying to write a simple script so that before the user has to select a row in the Gridview before being able to continue to the next Page. If they haven't selected a row and they press the button, the page only shows an error messge. As far as I know, none of the Validation controls will work with this type of event. Here's my code: (I left out namespaces for clarity)

1public partialclass Manage : System.Web.UI.Page2{3public class Verify4 {5private int verifyClick;6public int VerifyClick7 {8get9 {return verifyClick; }10set11 { verifyClick =value; }12 }13 }14 Verify selectAction =new Verify();1516public void Page_Load(object sender, EventArgs e)17 {1819 }20public void Button1_Click(object sender, EventArgs e)21 {22if (selectAction.VerifyClick != 1)23 {24 Label1.Text ="You must Select a Lesson before choosing to Continue";25 }26else27 {28 Response.Redirect("ManageContent.aspx?selectedLesson=" + GridView1.SelectedDataKey.Value.ToString());29 }30 }31public void GridView1_SelectedIndexChanged(object sender, EventArgs e)32 {33 selectAction.VerifyClick=1;34 }35}

I think it has something to do with where I change VerifyClick to 1. Something about its scope is not allowing me to place it under the GridView event, but I dont know where else I could place line 33 to get the same results

bump...please help

i couldn't understant how u r selecting a row, i mean what control u r using to select ? Linkbutton, radiobutton, checkbox ??

if possible paste ur GridView Html code here


Thank you so much for attempting to help.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="LessonId" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None" Style="position: relative"OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
 <Columns> <asp:BoundField DataField="CatId" HeaderText="Category" SortExpression="CatId"/>
<asp:BoundField DataField="LessonId" HeaderText="Lesson Number" InsertVisible="False" SortExpression="LessonId" />
<asp:BoundField DataField="LessonTitle" HeaderText="Lesson Title" SortExpression="LessonTitle" />
<asp:CommandField ShowDeleteButton=True" ShowEditButton="True" ShowSelectButton="True" />
 </Columns> 
</asp:GridView>
  

I figured it out. Now I can have a good laugh. I'm new to this stuff and still not used to postbacks. When a row is selected I didn't realize page was getting reposted so i had to change the code to detect what row was selected and compare it to default value of -1 (no row selected). Here's code: (silly me)

public partialclass Manage : System.Web.UI.Page{public class Verify {public int verifyClick;public int VerifyClick {get {return verifyClick; }set { verifyClick =value; } } } Verify selectAction =new Verify();public void Page_Load(object sender, EventArgs e) {if(Page.IsPostBack==true){if(GridView1.SelectedIndex!=-1){ selectAction.VerifyClick = 1; } } }public void Button1_Click(object sender, EventArgs e) {if (selectAction.VerifyClick != 1) { Label1.Text ="You must Select a Lesson before choosing to Continue"; }else { Response.Redirect("ManageContent.aspx?selectedLesson=" + GridView1.SelectedDataKey.Value.ToString()); } } }

No comments:

Post a Comment