Gridview Validation - c#

I am using Gridview and using Item Template text box. There are 5 text box and i am adding a new row dynamically on clicking on add row btn. in that row i have adding all text box which is empty. Now i want to validate that text box on btn click next.
now to do it?
I used required field validation its working for first time showing text box but when i am adding a new row text box its not causing the validation, I think there is some other way to give validation for dynamically added text box.
How could i validation my all dynamically added text box
Grid view which i am using..
<Columns >
<asp:TemplateField>
<ItemTemplate>
<div class="otherOthersTd">
<asp:Label ID="lblcnt" ForeColor="#136A96" Text='<%#Eval("Count") %>' runat="server" ></asp:Label>
<asp:Label ID="lblId" runat="server" text='<%#Eval("Id") %>' Visible="false"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' Width="88px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middle Name">
<ItemTemplate>
<asp:TextBox ID="txtMName" runat="server" Text='<%# Eval("MName") %>'
Width="88px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox ID="txtLName" runat="server" Text='<%# Eval("LName") %>'
Width="88px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Degree">
<ItemTemplate>
<asp:TextBox ID="txtDegree" runat="Server" Text='<%# Eval("Degree") %>'
Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:TextBox ID="txtTitle" runat="Server" Text='<%# Eval("Title") %>' Width="88px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox ID="txtEmail" runat="Server"
Text='<%# Eval("Email") %>' Width="88px" CausesValidation="True" ValidationGroup="a"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpr" runat="server" ErrorMessage="Invalid email id" ControlToValidate="txtEmail" ValidationGroup="a" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:Label ID="revexp" runat="server" > </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Institution">
<ItemTemplate>
<asp:TextBox ID="txtInstitution" runat="server" Text='<%#Eval("Institution") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:Button ID="btnNext" runat="server" ValidationGroup="a" CausesValidation="true" Text="Next" class="next btn" onclick="btnNext_Click"/>
<asp:TemplateField> <ItemTemplate>
<asp:Label ID="lblAuthor" runat="server" Text="Authorerror" Visible="false" ForeColor="Red" Font-Bold="True" Font-Size="X-Large">*</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="false" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle Font-Bold="false" ForeColor="#136A96" />
</asp:GridView>
my code...
protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals(""))
{
lstAuthors = (List<OtherAuthors>)Session["lstAuthors"];
if (lstAuthors.Count == 0)
{
OtherAuthors obj0 = new OtherAuthors();
obj0.Count = "Author 1:";
lstAuthors.Add(obj0);
}
int index=GridView1.Rows.Count-1;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox Name = GridView1.Rows[i].FindControl("txtName") as TextBox;
TextBox MName = GridView1.Rows[i].FindControl("txtMName") as TextBox;
TextBox LName = GridView1.Rows[i].FindControl("txtLName") as TextBox;
TextBox Degree = GridView1.Rows[i].FindControl("txtDegree") as TextBox;
TextBox Title = GridView1.Rows[i].FindControl("txtTitle") as TextBox;
TextBox Email = GridView1.Rows[i].FindControl("txtEmail") as TextBox;
TextBox Institution = GridView1.Rows[i].FindControl("txtInstitution") as TextBox;
if(Name!=null)
{
lstAuthors[i].Name = Name.Text;
lstAuthors[i].MName = MName.Text;
lstAuthors[i].LName = LName.Text;
lstAuthors[i].Degree = Degree.Text;
lstAuthors[i].Title = Title.Text;
lstAuthors[i].Email = Email.Text;
lstAuthors[i].Institution = Institution.Text;
}
}
OtherAuthors obj1 = new OtherAuthors();
obj1.Count = "Author "+(lstAuthors.Count+1).ToString()+":";
obj1.Name="";
lstAuthors.Add(obj1);
GridView1.DataSource = lstAuthors;
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows.Count - 1 == i)
GridView1.Rows[i].Cells[8].Visible = true;
else
GridView1.Rows[i].Cells[8].Visible = false;
}
Session["lstAuthors"] = lstAuthors;
}
MultipleModalitySelect1.hideChosebutton = true;
}
private bool IsValied()
{
bool error = false;
TextBox Name = GridView1.Rows[0].FindControl("txtName") as TextBox;
TextBox MName = GridView1.Rows[0].FindControl("txtMName") as TextBox;
TextBox LName = GridView1.Rows[0].FindControl("txtLName") as TextBox;
TextBox Degree = GridView1.Rows[0].FindControl("txtDegree") as TextBox;
TextBox Title = GridView1.Rows[0].FindControl("txtTitle") as TextBox;
TextBox Email = GridView1.Rows[0].FindControl("txtEmail") as TextBox;
TextBox Institution = GridView1.Rows[0].FindControl("txtInstitution") as TextBox;
Label lblAuthor = GridView1.Rows[0].FindControl("lblAuthor") as Label;
if (Name.Text.Length == 0 || LName.Text.Length == 0 || Degree.Text.Length == 0 ||Title.Text.Length == 0 || Email.Text.Length == 0 || Institution.Text.Length == 0)
{
lblAuthor.Visible = true;
error = true;
}
else
{
lblAuthor.Visible = false;
}
}

You have used the validation group's ValidationGroup="a" on your Next button, but you have not used that on the Required Field Validator. But you have used it in the Email validator.
You have to be consistent with the Validation control and whether to put validation on all controls and button controls for it to work.
<asp:Button ID="btnNext" runat="server" ValidationGroup="a" CausesValidation="true" Text="Next" class="next btn" onclick="btnNext_Click"/>

I think you should start a new page and test with a much smaller case scenario and get that working first. Then add more boxes.
If you still need help with that smaller page, post it for help - it will be easier for us to understand it.

Related

how to set compare validatior for footer table text boxes?

I have a grid view with two footer text boxex,
<asp:GridView ID="grdmaster" runat="server" AutoGenerateColumns="false" ShowFooter="true" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtdescription" runat="server" > </asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltotal" Font-Bold="true" runat="server" Text="Total" > </asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Debit">
<ItemTemplate>
<asp:TextBox ID="txtdebit" runat="server" AutoPostBack="true" OnTextChanged="txtdebit_TextChanged"> </asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtdebit1" Font-Bold="true" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credit">
<ItemTemplate>
<asp:TextBox ID="txtcredit" runat="server" AutoPostBack="true" OnTextChanged="txtcredit_TextChanged"> </asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcredit2" Font-Bold="true" runat="server"></asp:Text>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btndelete" runat="server" class="btn red icn-only" OnClick="btndelete_Click"><i class="icon-remove icon-white"></i> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
here I want to compare the footer Textebox txtdebit1 and txtcredit2 values are same or not.How can I set compare validator for.I followed some methods from google but got error message like Compare validator could not find control to validate text box.Is it possible to set compare validator for footer table text box?
Please try below,
protected void grdmaster_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridFooterItem)
{
GridDataItem item = (GridDataItem)e.Item;
TextBox txtdebit1 = item.FindControl("txtdebit1") as TextBox;
TextBox txtcredit2 = item.FindControl("txtcredit2") as TextBox;
TableCell cell = (TableCell)txtdebit1.Parent;
CompareValidator val = new CompareValidator();
val.ControlToCompare = txtcredit2.ID;
val.ControlToValidate = txtdebit1.ID;
val.Operator = ValidationCompareOperator.LessThan;
val.Display = ValidatorDisplay.Dynamic;
val.ErrorMessage = "Error message";
cell.Controls.Add(val);
}
}

Bind a Dropdown in Gridview When Selected Index Changes on Dropdown in the same Gridview

I have a gridview with template fields consisting of dropdowns and textboxes. 1 dropdown will be a so-called "master" dropdown that when the selected index changes, other dropdowns in the same row will be databinded using the selected value of the master dropdown.
I have this code for my ASPX
<asp:GridView ID="gvEducations" runat="server" AutoGenerateColumns="false"
DataKeyNames="file2_id" onrowcancelingedit="gvEducations_RowCancelingEdit"
onrowdatabound="gvEducations_RowDataBound"
onrowdeleting="gvEducations_RowDeleting" onrowediting="gvEducations_RowEditing"
onrowupdating="gvEducations_RowUpdating">
<Columns>
<asp:BoundField HeaderText="file2_id" DataField="file2_id" ReadOnly="true" Visible="False"/>
<asp:TemplateField HeaderText="Education Establishment">
<ItemTemplate>
<%# Eval("education_establishment_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditEducationEstablishment" runat="server"
OnSelectedIndexChanged="ddlEditEducationEstablishment_SelectedIndexChanged">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="School">
<ItemTemplate>
<%# Eval("school_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditSchool" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="School Others" Visible="false">
<ItemTemplate>
<%# Eval("school_others") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditSchoolOthers" runat="server" MaxLength="81"
Text='<%# Eval("school_others") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Start Date">
<ItemTemplate>
<%# Convert.ToDateTime(Eval("start_date")).ToString("d")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditStartDate" runat="server" type="date"
Text='<%# Bind("start_date", "{0:yyyy-MM-dd}") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="End Date">
<ItemTemplate>
<%# Convert.ToDateTime(Eval("end_date")).ToString("d")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditEndDate" runat="server" type="date"
Text='<%# Bind("end_date", "{0:yyyy-MM-dd}") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch Of Study 1">
<ItemTemplate>
<%# Eval("branch_of_study_1_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditBranchOfStudy1" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Certificate">
<ItemTemplate>
<%# Eval("certificate_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditCertificate" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Appraisal">
<ItemTemplate>
<%# Eval("course_appraisal") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditCourseAppraisal" runat="server" MaxLength="30"
Text='<%# Eval("course_appraisal") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch Of Study 2">
<ItemTemplate>
<%# Eval("branch_of_study_2_code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditBranchOfStudy2" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" UseSubmitBehavior="false"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Update" UseSubmitBehavior="false"/>
<asp:Button ID="btnCanel" runat="server" Text="Cancel" CommandName="Cancel" UseSubmitBehavior="false"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" UseSubmitBehavior="false"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind (RowDataBound Gridview events)
protected void gvEducations_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvEducations.EditIndex == e.Row.RowIndex)
{
int file2Id = int.Parse(gvEducations.DataKeys[e.Row.RowIndex].Value.ToString());
DataTable dt = new DataTable();
dt = file2BLO.SelectSpecificFile2(file2Id);
DropDownList ddlEditEducationEstablishment =
(DropDownList)e.Row.FindControl("ddlEditEducationEstablishment");
ddlEditEducationEstablishment.DataSource =
educationEstablishmentBLO.SelectAllEducationEstablishment();
ddlEditEducationEstablishment.DataValueField = "education_establishment_code";
ddlEditEducationEstablishment.DataTextField = "education_establishment";
ddlEditEducationEstablishment.DataBind();
ddlEditEducationEstablishment.SelectedValue =
dt.Rows[0]["education_establishment_code"].ToString();
string educationEstablishmentCode = ddlEditEducationEstablishment.SelectedValue;
DropDownList ddlEditSchool = (DropDownList)e.Row.FindControl("ddlEditSchool");
ddlEditSchool.DataSource =
schoolBLO.SelectSchoolOfEstablishment(educationEstablishmentCode);
ddlEditSchool.DataValueField = "school_code";
ddlEditSchool.DataTextField = "school";
ddlEditSchool.DataBind();
ddlEditSchool.SelectedValue = dt.Rows[0]["school_code"].ToString();
DropDownList ddlEditBranchOfStudy1 = (DropDownList)e.Row.FindControl
("ddlEditBranchOfStudy1");
ddlEditBranchOfStudy1.DataSource = branchOfStudyBLO.
SelectBranchOfEstablishment(educationEstablishmentCode);
ddlEditBranchOfStudy1.DataValueField = "branch_of_study_code";
ddlEditBranchOfStudy1.DataTextField = "branch_of_study";
ddlEditBranchOfStudy1.DataBind();
ddlEditBranchOfStudy1.SelectedValue = dt.Rows[0]["branch_of_study_1_code"].ToString();
DropDownList ddlEditCertificate = (DropDownList)e.Row.FindControl
("ddlEditCertificate");
ddlEditCertificate.DataSource = certificateBLO.
SelectCertificateOfEstablishment(educationEstablishmentCode);
ddlEditCertificate.DataValueField = "certificate_code";
ddlEditCertificate.DataTextField = "certificate";
ddlEditCertificate.DataBind();
ddlEditCertificate.SelectedValue = dt.Rows[0]["certificate_code"].ToString();
DropDownList ddlEditBranchOfStudy2 = (DropDownList)e.Row.
FindControl("ddlEditBranchOfStudy2");
ddlEditBranchOfStudy2.DataSource = branchOfStudyBLO.
SelectBranchOfEstablishment(educationEstablishmentCode);
ddlEditBranchOfStudy2.DataValueField = "branch_of_study_code";
ddlEditBranchOfStudy2.DataTextField = "branch_of_study";
ddlEditBranchOfStudy2.DataBind();
ddlEditBranchOfStudy2.SelectedValue = dt.Rows[0]["branch_of_study_1_code"].ToString();
}
}
This is what I have tried:
protected void ddlEditEducationEstablishment_SelectedIndexChanged(object sender, EventArgs e)
{
//if (e.Row.RowType == DataControlRowType.DataRow && gvEducations.EditIndex == e.Row.RowIndex)
//{
// DropDownList ddlEditEducationEstablishment =
// (DropDownList)e.Row.FindControl("ddlEditEducationEstablishment");
// string educationEstablishmentCode = ddlEditEducationEstablishment.SelectedValue;
// DropDownList ddlEditSchool = (DropDownList)e.Row.FindControl("ddlEditSchool");
// ddlEditSchool.DataSource =
// schoolBLO.SelectSchoolOfEstablishment(educationEstablishmentCode);
// ddlEditSchool.DataValueField = "school_code";
// ddlEditSchool.DataTextField = "school";
// ddlEditSchool.DataBind();
// DropDownList ddlEditBranchOfStudy1 = (DropDownList)e.Row.FindControl
// ("ddlEditBranchOfStudy1");
// ddlEditBranchOfStudy1.DataSource = branchOfStudyBLO.
// SelectBranchOfEstablishment(educationEstablishmentCode);
// ddlEditBranchOfStudy1.DataValueField = "branch_of_study_code";
// ddlEditBranchOfStudy1.DataTextField = "branch_of_study";
// ddlEditBranchOfStudy1.DataBind();
// DropDownList ddlEditCertificate = (DropDownList)e.Row.FindControl
// ("ddlEditCertificate");
// ddlEditCertificate.DataSource = certificateBLO.
// SelectCertificateOfEstablishment(educationEstablishmentCode);
// ddlEditCertificate.DataValueField = "certificate_code";
// ddlEditCertificate.DataTextField = "certificate";
// ddlEditCertificate.DataBind();
// DropDownList ddlEditBranchOfStudy2 = (DropDownList)e.Row.
// FindControl("ddlEditBranchOfStudy2");
// ddlEditBranchOfStudy2.DataSource = branchOfStudyBLO.
// SelectBranchOfEstablishment(educationEstablishmentCode);
// ddlEditBranchOfStudy2.DataValueField = "branch_of_study_code";
// ddlEditBranchOfStudy2.DataTextField = "branch_of_study";
// ddlEditBranchOfStudy2.DataBind();
//}
}
What I want is that when I select in the first dropdown, the other dropdowns will be databinded again using the selected value in the first dropdown. Refer to the image below.
Like I said in my comment, you need to find the GridViewRow that your DropDownList is contained within. With that row, you can then find all your other DropDownLists. You are on the right track by finding each of the other DropDownLists, but you aren't looking for them in the correct location.
protected void ddlEditEducationEstablishment_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the master DropDownList and its value
DropDownList ddlEditEducationEstablishment = (DropDownList)sender;
string educationEstablishmentCode = ddlEditEducationEstablishment.SelectedValue;
// Get the GridViewRow in which this master DropDownList exists
GridViewRow row = (GridViewRow)ddlEditEducationEstablishment.NamingContainer;
// Find all of the other DropDownLists within the same row and bind them
DropDownList ddlEditSchool = (DropDownList)row.FindControl("ddlEditSchool");
ddlEditSchool.DataSource = schoolBLO.SelectSchoolOfEstablishment(educationEstablishmentCode);
ddlEditSchool.DataValueField = "school_code";
ddlEditSchool.DataTextField = "school";
ddlEditSchool.DataBind();
// etc...
}

Accessing DetailsView fields

I've started creating an online quiz. I am using a DetailsView linked to a sqlDataSource.
Here's my code:
<asp:DetailsView ID="QuestionDetails" runat="server" AutoGenerateRows="False" DataKeyNames="QuestionID,QuizID" DataSourceID="SqlDataSource_Quiz">
<Fields>
<asp:TemplateField HeaderText="Text_Eng" SortExpression="Text_Eng" ShowHeader="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:RadioButton ID="Option1" AccessKey="1" runat="server" Text='<%# Bind("Opt_1") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged" />
<br />
<asp:RadioButton ID="Option2" AccessKey="2" runat="server" Text='<%# Bind("Opt_2") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
<br />
<asp:RadioButton ID="Option3" AccessKey="3" runat="server" Text='<%# Bind("Opt_3") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
<br />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Note that i am not displaying all data retrieved from the sqlDataSource (there are other fields/columns as well) in the DetailsView.
Now, in my code behind, i am showing the next record by changing the PageIndex of the DetailsView upon clicking a button (NextButton) but at the same time, i need to retrieve some data of the currently displayed record so I've put the following in the button's click handler:
protected void NextButton_Click(object sender, EventArgs e)
{
try
{
// Save off previous answers
DataRowView dr = (DataRowView)QuestionDetails.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = dr["QuestionID"].ToString();
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = selectedOptionRB.AccessKey.ToString();
ArrayList al = (ArrayList)Session["userAnswers"];
al.Add(a);
Session.Add("userAnswers", al);
}
catch (Exception ex)
{
Label2.Text = "Exception!";
}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Enabled = false;
}
else
{
QuestionDetails.PageIndex += 1;
NextButton.Enabled = false;
}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Text = "Finish the Quiz";
}
}
So when i run the code, i get "Exception!" displayed in my Label2 which i setup for testing.
What am i doing wrong?

Populate textbox from database or label

I'm having trouble figuring this out and it shouldn't be too difficult. I want to populate my textbox from my database or from the label (The labels are pulling the information from the database already, so essentially copying what's in the label to the textbox). I'm using a GridView and here is the code that I've been trying:
C#:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox timeR = GridView1.Rows[i].FindControl("rTime") as TextBox;
Label timeRL = GridView1.Rows[i].FindControl("labelRunScore") as Label;
if (timeR.Text == "")
{
timeR.Text = timeRL.Text;
}
}
.aspx:
<asp:TemplateField HeaderText = "Run Time">
<ItemTemplate>
<asp:Label ID="labelRunScore" Visible="true" runat="server" Text='<%# Eval("rTime") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Time">
<ItemTemplate>
<div style="display:none"> <asp:TextBox ID="rTime" runat="server" type="number" Text='<%# Eval("rTime") %>' ></asp:TextBox></div>
<input onblur="document.getElementById('<%# ((GridViewRow)Container).FindControl("rTime").ClientID %>').value = this.value"
type="number" style="width: 100px; height: 31px;" />
</ItemTemplate>
</asp:TemplateField>

Hide asp.net GridView row with condition

This is my grid
<asp:GridView ID="gridProduct" runat="server"
AutoGenerateColumns="false"
ShowFooter="true"
onrowcancelingedit="gridProduct_RowCancelingEdit"
onrowdeleting="gridProduct_RowDeleting" onrowediting="gridProduct_RowEditing"
onrowupdating="gridProduct_RowUpdating"
onrowcommand="gridProduct_RowCommand"
onrowdatabound="gridProduct_RowDataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="buttonUpdate" CommandName="Update" runat="server" ToolTip="Update" Text="Update" />
<asp:Button ID="buttonCancel" CommandName="Cancel" runat="server" ToolTip="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="buttonEdit" CommandName="Edit" runat="server" Text="Edit" ToolTip="Edit"/>
<asp:Button ID="buttonDelete" CommandName="Delete" runat="server" Text="Delete" ToolTip="Delete"/>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="buttonAdd" runat="server" Text="Ajouter" CommandName="AddNew" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="#Piece">
<EditItemTemplate>
<asp:Label ID="labelEditPiece" runat="server" Text='<%#Eval("Piece") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelItemPiece" runat="server" Text='<%#Eval("Piece") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="dropDownListPartsFooter" runat="server" DataTextField="Nom" DataValueField="ID_AchatTemplate">
</asp:DropDownList>
ControlToValidate="txtBoxPiece" Text="*" ValidationGroup="validaiton"/>--%>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Series">
<EditItemTemplate>
<asp:Label ID="labelEditSeries" runat="server" Text='<%#Eval("Series") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelItemSeries" runat="server" Text='<%#Eval("Series") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBoxSeries" runat="server"/>
<asp:RequiredFieldValidator ID="fieldValidSeries" runat="server" ControlToValidate="txtBoxSeries" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
.... </asp:TemplateField>
</Columns>
This is my page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PsaDataSet psaList = new PsaDataSet();
ViewState.Remove("psaList");
ViewState.Add("psaList", psaList);
ViewState.Add("psaUid", Guid.NewGuid());
if (psaList.PsaLink.DefaultView.Count == 0)
{
// Patch for view footer row when no data
PsaDataSet.PsaLinkDataTable tmpList = new PsaDataSet.PsaLinkDataTable();
PsaDataSet.PsaLinkRow tmpItem = tmpList.NewPsaLinkRow();
tmpItem.PsaUid = (Guid)ViewState["psaUid"];
tmpItem.PsaProductUid = Guid.Empty;
tmpItem.ProductId = 1;
tmpItem.Series = "test";
tmpItem.Rev = "test";
tmpItem.Firmware = "test";
tmpList.AddPsaLinkRow(tmpItem);
tmpList.AcceptChanges();
ViewState.Add("series", tmpItem.Series);
gridProduct.DataSource = tmpList;
gridProduct.DataBind();
}
}
else
{
//BindGrid((PsaDataSet)ViewState["psaList"], false);
}
}
private void BindGrid(PsaDataSet psaList, bool mustDataBind)
{
gridProduct.DataSource = psaList.PsaLink;
//if (mustDataBind)
//{
gridProduct.DataBind();
//}
}
This my onrowdatabound="gridProduct_RowDataBound"> method
protected void gridProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["style"] = "display:none";
}
}
}
I want to add a condition (Based whit a test value inserted in page load) in the if(protected void gridProduct_RowDataBound method) for hiding just one time on page load??
Thank Frank!
I belive you can get the DataBoundItem from the row which is a type of "PsaDataSet.PsaLinkRow" and use that you get the ProductId, Series, etc. and do the condition that you require. Also, you have and if condition inside an if condition with the same condition for both. You only need one.

Categories

Resources