I am creating exam website in vs2010 with asp.net C# 4.0,
in which I have the detailsview in questions page
<asp:DetailsView ID="questiondetail" runat="server" AutoGenerateRows="False"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Horizontal"
Height="69px" Width="100%">
<AlternatingRowStyle BackColor="#F7F7F7" />
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<Fields>
<asp:TemplateField HeaderText="Question" SortExpression="question">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("question") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("question") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("question") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="5px" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="A)" SortExpression="a">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("a") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("a") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="ans"
Text='<%# Eval("a") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="B)" SortExpression="b">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("b") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("b") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="ans"
Text='<%# Eval("b") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="C)" SortExpression="c">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("c") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("c") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton3" runat="server" GroupName="ans"
Text='<%# Eval("c") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="D)" SortExpression="d">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("d") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("d") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton4" runat="server" GroupName="ans"
Text='<%# Eval("d") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:BoundField DataField="correctans" HeaderText="correctans"
SortExpression="correctans" Visible="False" />
</Fields>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
</asp:DetailsView>
code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
questiondetail.DataBind();
}
}
string userans = "Not selected";
protected void btnnext_Click(object sender, EventArgs e)
{
try
{
questiondetail.DataBind();
DataRowView dr = (DataRowView)questiondetail.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = questiondetail.PageIndex.ToString();
a.CorrectAnswer = dr.Row["correctans"].ToString();
//get user ans
questiondetail.DefaultMode = DetailsViewMode.Insert;
RadioButton r1 = questiondetail.FindControl("RadioButton1") as RadioButton;
RadioButton r2 = questiondetail.FindControl("RadioButton2") as RadioButton;
RadioButton r3 = questiondetail.FindControl("RadioButton3") as RadioButton;
RadioButton r4 = questiondetail.FindControl("RadioButton4") as RadioButton;
if (r1.Checked)
{
userans = "A";
}
else if (r2.Checked)
{
userans = "B";
}
else if (r3.Checked)
{
userans = "C";
}
else if (r4.Checked)
{
userans = "D";
}
else
{
userans = "Not selected";
}
questiondetail.DefaultMode = DetailsViewMode.ReadOnly;
a.UserAnswer = userans;
ArrayList al = (ArrayList)Session["AnswerList"];
al.Add(a);
Session.Add("AnswerList", al);
}
catch (Exception ex)
{
lblcatch.Text = ex.Message;
}
if (questiondetail.PageIndex == questiondetail.PageCount - 1)
{
Response.Redirect("Result.aspx");
}
else
{
questiondetail.PageIndex++;
}
if (questiondetail.PageIndex == questiondetail.PageCount - 1)
{
btnnext.Text = "Finish";
}
}
the problem is that on result page it shows every answer is Not selected
so how get that which radiobutton is checked in detailsview
Try to remove the questiondetail.DefaultMode = DetailsViewMode.Insert; and questiondetail.DefaultMode = DetailsViewMode.ReadOnly;
Related
I have a grid view control in my page that indexes all the Lecture Contents. I have added a Text Box control and a Link Button. But for the Lectures that have no Content yet, the Grid View does not appear.
Have tried many references, as a result the footer has appeared but I am not able to insert data using the Controls in the footer.
My main objective is to make the Controls behave same in both scenarios.
Here is the Grid View Code.
`<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="No Contents found for this lecture." ShowFooter="true" HeaderStyle-Wrap="false" Width="70%" CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText=" LectureID " SortExpression="lecc_id">
<ItemTemplate>
<asp:Label ID="lbllecc_id" runat="server" Text='<%# Bind("lecc_id") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbllecc_id" runat="server" T`enter code here`ext="Create New">
</asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Content " SortExpression="lecc_contents">
<EditItemTemplate>
<asp:TextBox ID="tblecc_contents" runat="server" Text='<%# Bind("lecc_contents") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbllecc_contents" runat="server" Text='<%# Bind("lecc_contents") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcontent" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfcontent" runat="server" ErrorMessage="Content field cannot be blank." ControlToValidate="txtcontent" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:LinkButton ID="createcontent" runat="server" OnClick="createcontent" CssClass="btn-sm btn-default">Insert Content</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</EmptyDataTemplate>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" Font-Size="17px" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" Font-Bold="False" Font-Size="14px" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>`
and the code behind functions for grid view.
`protected void Page_Load(object sender, EventArgs e){
if (Session["fac_username"] != null)
{
String username = Convert.ToString(Session["fac_username"]);
if (!IsPostBack)
{
BindGridViewData();
}
}
else
{
Response.Redirect("~/Account/Login.aspx");
}
}protected void BindGridViewData(){
int lecID = Convert.ToInt32(Session["lecID"]);
GridView1.DataSource = DataAccessLayer.getAllContents(lecID);
GridView1.DataBind();} protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){
}
protected void createcontent(object sender, EventArgs e){
int lecID = Convert.ToInt32(Session["lecID"]);
con.Open();
SqlCommand command = new SqlCommand("insert into uni_lect_content(lecc_id, lecc_contents) values(#lecc_id, #lecc_contents)", con);
command.Parameters.AddWithValue("#lecc_id", lecID);
command.Parameters.AddWithValue("#lecc_contents", ((TextBox)GridView1.FooterRow.FindControl("txtcontent")).Text);
command.ExecuteNonQuery();
con.Close();
lblmsg.Text = "New Course Successfully Created..!";}`
DataAccessLayer class's getAllConents() method is here.
`public class AllContents
{
public long lecc_id { get; set; }
public string lecc_contents { get; set; }
}
public class DataAccessLayer
{
public static List<AllContents> getAllContents( int lecID){
List<AllContents> listAll = new List<AllContents>();
string cs = ConfigurationManager.ConnectionStrings["ILMSConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand com = new SqlCommand("select lecc_id, lecc_contents from uni_lect_content, uni_lectures, uni_offered_courses where uni_lect_content.lecc_id = uni_lectures.lec_id and uni_lectures.cours_offer_id = uni_offered_courses.cours_offer_id and lecc_id = '"+lecID+"'", con);
con.Open();
SqlDataReader rdr = com.ExecuteReader();
while (rdr.Read())
{
AllContents obj = new AllContents();
obj.lecc_id = Convert.ToInt32(rdr["lecc_id"]);
obj.lecc_contents = rdr["lecc_contents"].ToString();
listAll.Add(obj);
}
}
return listAll;
}`
Help me out of this situation.
Thanks in advance.
FRONT END GRDIVIEW
<asp:GridView ID="grdview" ShowFooter="true" OnRowEditing="grdview_RowEditing" OnRowCancelingEdit="grdview_RowCancelingEdit" OnRowCommand="grdview_RowCommand" OnRowUpdating="grdview_RowUpdating" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="SR No.">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderText="Emp Name">
<ItemTemplate>
<asp:Label ID="elblempname" runat="server" Text='<%#Eval("EmpName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="etxtempname" runat="server" Text='<%#Eval("EmpName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtempname" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderText="Emp No">
<ItemTemplate>
<asp:Label ID="elblempno" runat="server" Text='<%#Eval("EmpNo") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="etxtempno" runat="server" Text='<%#Eval("EmpNo") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtempno" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderText="Email">
<ItemTemplate>
<asp:Label ID="elblempemail" runat="server" Text='<%#Eval("Email_Id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="etxtempemail" runat="server" Text='<%#Eval("Email_Id") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtempemail" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderText="Age">
<ItemTemplate>
<asp:Label ID="elblempage" runat="server" Text='<%#Eval("Age") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="etxtempage" runat="server" Text='<%#Eval("Age") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtempage" runat="server" ></asp:TextBox>
<asp:Button ID="btnAdd" CommandName="AddNew" runat="server" Text="Add" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
BACKEND CODE
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
grdview.DataSource = ds;
grdview.DataBind();
int columncount = grdview.Rows[0].Cells.Count;
grdview.Rows[0].Cells.Clear();
grdview.Rows[0].Cells.Add(new TableCell());
grdview.Rows[0].Cells[0].ColumnSpan = columncount;
grdview.Rows[0].Cells[0].Text = "No Records Found";
if (e.CommandName == "AddNew")
{
TextBox ftxtempname = (TextBox)grdview.FooterRow.FindControl("ftxtempname");
TextBox ftxtempno = (TextBox)grdview.FooterRow.FindControl("ftxtempno");
TextBox ftxtempemail = (TextBox)grdview.FooterRow.FindControl("ftxtempemail");
TextBox ftxtempage = (TextBox)grdview.FooterRow.FindControl("ftxtempage");
using (con)
{
SqlCommand cmd = new SqlCommand("usp_add_upd_emptb", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#EmpName", ftxtempname.Text);
cmd.Parameters.AddWithValue("#EmpNo", ftxtempno.Text);
cmd.Parameters.AddWithValue("#Desig", ftxtempage.Text);
cmd.Parameters.AddWithValue("#Email", ftxtempemail.Text);
con.Open();
cmd.ExecuteNonQuery();
}
This is a working complete code.
I'm creating a page to enter employee's leave details in a dynamic gridview. Entry of data will be done by the from the footer of the gridview. When I try to get the values from the footer, all the text values as well as other values are coming as null or "".
Here is the code of RowCommand:
protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("ADD"))
{
TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");
RadioButtonList rblFooter = (RadioButtonList)gvEmployeeDetails.FooterRow.FindControl("rblFooter");
TextBox txtAddDoj = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddDoj");
TextBox txtAddLeaves = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddLeaves");
TextBox txtAddDesignation = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddDesignation");
conn.Open();
string cmdstr = "insert into tblLeaves(EmpId,IsActive,DOJ,Leaves,Designation) values(#EmpId,#IsActive,#DOJ,#Leaves,#Designation)";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("#EmpId", txtAddEmpID.Text);
cmd.Parameters.AddWithValue("#IsActive", rblFooter.SelectedValue);
cmd.Parameters.AddWithValue("#DOJ", txtAddDoj.Text);
cmd.Parameters.AddWithValue("#Leaves", txtAddLeaves.Text);
cmd.Parameters.AddWithValue("#Designation", txtAddDesignation.Text);
cmd.ExecuteNonQuery();
conn.Close();
BindData();
}
}
And below is the gridview:
<asp:GridView ID="gvEmployeeDetails" runat="server" Width="100%"
AutoGenerateColumns="False" ShowFooter="True"
onrowcommand="gvEmployeeDetails_RowCommand"
onrowdeleting="gvEmployeeDetails_RowDeleting"
onrowupdating="gvEmployeeDetails_RowUpdating"
onrowcancelingedit="gvEmployeeDetails_RowCancelingEdit"
onrowediting="gvEmployeeDetails_RowEditing" style="text-align: center" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns>
<asp:TemplateField HeaderText="Employee ID">
<FooterTemplate>
<asp:TextBox ID="txtAddEmpID" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooterEmpID" runat="server" ControlToValidate="txtAddEmpID" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "EmpId") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "EmpId") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active">
<EditItemTemplate>
<asp:RadioButtonList ID="rblEdit" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="rblFooter" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="rfvFooterIsActive" runat="server" ControlToValidate="rblFooter" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "IsActive") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Of Joining">
<ItemTemplate>
<asp:Label ID="lblDoj" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DOJ") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDoj" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DOJ") %>'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="txtDoj_CalendarExtender" runat="server" BehaviorID="txtDoj_CalendarExtender" TargetControlID="txtDoj" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddDoj" runat="server" MaxLength="10" OnTextChanged="txtAddDoj_TextChanged" ></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="txtAddDoj_CalendarExtender" runat="server" BehaviorID="txtAddDoj_CalendarExtender" TargetControlID="txtAddDoj" />
<asp:RequiredFieldValidator ID="rfvFooterDOJ" runat="server" ControlToValidate="txtAddDoj" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No. Of Leaves">
<ItemTemplate>
<asp:Label ID="lblLeaves" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Leaves") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLeaves" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Leaves") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddLeaves" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooterLeaves" runat="server" ControlToValidate="txtAddLeaves" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Designation") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Designation") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddDesignation" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooterDesig" runat="server" ControlToValidate="txtAddDesignation" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/Edit.png" Height="32px" Width="32px"/>
<asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Delete.png" Height="32px" Width="32px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/icon-update.png"/>
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/icon-Cancel.png"/>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD" Text="Add" Width="100px"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
Please correct me where I'm going wrong.
You are not using sender in your find control code line. Try to replace below line.
TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");
this.
protected void gridview_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Add") {
TextBox txtAddEmpID = (TextBox)((GridView)sender).FooterRow.FindControl("txtAddEmpID");
}
}
values of selected items in dropdownlist(edit gridview) are returning NULL. It should return selected values..
Code in aspx.cs file::
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
DropDownList ct = (DropDownList)row.FindControl("DropDownList6");
string ctype = ct.SelectedItem.Text;
string sql1 = ("Select Case_Type_Value FROM Case_Type where Case_Type_Text ='" + ctype + "' ");
DropDownList cs = (DropDownList)row.FindControl("DropDownList3");
string cstatus = cs.SelectedItem.Text;
string sql2 = ("Select Case_Status_Value FROM Case_Status where Case_Status_Text ='" + cstatus + "' ");
SqlConnection con= new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlCommand cmd1 = new SqlCommand(sql1, con);
SqlCommand cmd2 = new SqlCommand(sql2, con);
con.Open();
int v1 = Convert.ToInt32(cmd1.ExecuteScalar());
int v2 = Convert.ToInt32(cmd2.ExecuteScalar());
con.Close();
SqlCommand cmd = new SqlCommand("UPDATE Table SET Case_Type = #Case_Type, Case_Status = #Case_Status, con);
cmd.Parameters.AddWithValue("#Case_Type", v1);
cmd.Parameters.AddWithValue("#Case_Status", v2);
Below is the aspx code for the page:
<asp:GridView ID="GridView1" runat="server" CellPadding="5" ForeColor="#333333" width="1000px"
GridLines="None" OnPageIndexChanging="gridView_PageIndexChanging"
AllowSorting="True" OnSorting="gridView_Sorting" AutoGenerateColumns="False" OnRowUpdating="GridView1_RowUpdating"
BorderStyle="Outset" CellSpacing="1" Font-Names="Cambria"
Font-Size="Small" AllowPaging="True" ShowFooter="True"
ShowHeaderWhenEmpty="True"
DataSourceID="SqlDataSource1" onselectedindexchanged="GridView1_SelectedIndexChanged"
>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Case Number" SortExpression="case_number">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("case_number") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("case_number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Case Name" SortExpression="case_name">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("case_name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("case_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Case Type" SortExpression="Case_Type_Text">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Case_Type_Text"
DataValueField="Case_Type_Text" SelectedValue='<%# Bind("Case_Type_Text") %>'>
<asp:ListItem>--Select--</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Case_Type_Text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Case Status" SortExpression="Case_Status_Text">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource3" DataTextField="Case_Status_Text" DataValueField="Case_Status_Text"
SelectedValue='<%# Bind("Case_Status_Text") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Case_Status_Text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned Date" SortExpression="assigned_date">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("assigned_date", "{0:d}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server"
Text='<%# Bind("assigned_date", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned To" SortExpression="assigned_to">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList7" runat="server"
DataSourceID="SqlDataSource4" DataTextField="User_Name"
DataValueField="User_Name" SelectedValue='<%# Bind("assigned_to") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("assigned_to") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Withdrawn" SortExpression="date_withdrawn">
<EditItemTemplate>
<cc1:DatePicker ID="DatePicker5" runat="server" DateFormat="yyyy-MM-dd"
CalendarDate='<%# Bind("date_withdrawn") %>' TextCssClass="" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server"
Text='<%# Bind("date_withdrawn", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Delivered" SortExpression="date_delivered">
<EditItemTemplate>
<cc1:DatePicker ID="DatePicker7" runat="server"
CalendarDate='<%# Bind("date_delivered") %>' TextCssClass=""
/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server"
Text='<%# Bind("date_delivered", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QC By" SortExpression="qc_by">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server"
DataSourceID="SqlDataSource4" DataTextField="User_Name" DataValueField="User_Name"
SelectedValue='<%# Bind("qc_by") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("qc_by") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QC Date" SortExpression="qc_date">
<EditItemTemplate>
<cc1:DatePicker ID="DatePicker6" runat="server"
CalendarDate='<%# Bind("qc_date") %>' TextCssClass=""
/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("qc_date", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Additional Notes">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("additional_notes") %>' TextMode="MultiLine"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("additional_notes") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ButtonType="Button"
CausesValidation="False" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Try likes this ,
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
DropDownList ct = (DropDownList)row.Cells[2].FindControl("DropDownList6");
string ctype = ct.SelectedValue;
DropDownList cs = (DropDownList)row.Cell[3].FindControl("DropDownList3");
string cstatus = cs.SelectedValue;
GridViewRow row = grdVw.Rows[e.RowIndex];
String str = Convert.ToString(((DropDownList)(row.Cells[2].Controls[0])).SelectedItem);
Get your cell number ,Numbering for cells start from zero so it will be 2 for DropDownList6 in your code .
Please check and verify if it works !
How can I find the controls lblCategoryName and ddlBlogCategory?
Here is my code:
protected void grdRetailStore_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblAccount = (Label)e.Row.FindControl("lblCategoryName");
string s = lblAccount.Text;
DropDownList ddlBlogCategory = (DropDownList)e.Row.FindControl("ddlBlogCategoty");
DAO.DAO dao_category = new DAO.DAO();
DataTable dt_blog_cat = dao_category.SelectCategory();
if (ddlBlogCategory != null)
{
// ddlBlogCategory.Items.Clear();
ddlBlogCategory.DataSource = dao_category.SelectCategory();
ddlBlogCategory.DataTextField = "CategoryName";
ddlBlogCategory.DataValueField = "BlogCategoryID";
ddlBlogCategory.DataBind();
}
}
}
<asp:GridView ID="grdRetailStore" runat="server" AutoGenerateColumns="false" DataKeyNames="PortalId" OnRowEditing="grdRetailStore_RowEditing"
OnRowCancelingEdit="grdRetailStore_RowCancelingEdit" OnRowDataBound="grdRetailStore_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("PortalId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("PortalId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AdvertiserName">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:TextBox ID="txtAdvName" runat="server" Text='<%# Bind("AdvertiserName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblAdvName" runat="server" Text='<%# Bind("AdvertiserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PromoCategory">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<%-- <asp:DropDownList ID="" runat="server" DataTextField="Account_Name" DataValueField="Account_Id"
AppendDataBoundItems="true">
<%-- <asp:ListItem Selected="True" Text="---Select---" Value="0"></asp:ListItem></asp:DropDownList> --%>
<asp:TextBox ID="txtPromoCat" runat="server" Text='<%# Bind("PromoCatId") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPromoCat" runat="server" Text='<%# Bind("PromoCatId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Start Date">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:TextBox ID="txtPromostartdate" runat="server" Text='<%# Bind("PromoStartDate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPromostartdate" runat="server" Text='<%# Bind("PromoStartDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="End Date">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:TextBox ID="txtPromoenddate" runat="server" Text='<%# Bind("PromoEndDate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPromoenddate" runat="server" Text='<%# Bind("PromoEndDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Html Code">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:TextBox ID="txtHtml" runat="server" Text='<%# Bind("HtmlCode") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblHtml" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryName">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:DropDownList ID="ddlBlogCategoty" runat="server" DataTextField="CategoryName" DataValueField="BlogCategoryID"
AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="---Select---" Value="0"></asp:ListItem></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" OnClientClick="return ValidateUpdate();"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Change your code behind and include check of RowState.
protected void grdRetailStore_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal)
{
Label lblAccount = (Label)e.Row.FindControl("lblCategoryName");
string s = lblAccount.Text;
}
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddlBlogCategory = (DropDownList)e.Row.FindControl("ddlBlogCategoty");
if (ddlBlogCategory != null)
{
// ddlBlogCategory.Items.Clear();
ddlBlogCategory.DataSource = dao_category.SelectCategory();
ddlBlogCategory.DataTextField = "CategoryName";
ddlBlogCategory.DataValueField = "BlogCategoryID";
ddlBlogCategory.DataBind();
}
}
DAO.DAO dao_category = new DAO.DAO();
DataTable dt_blog_cat = dao_category.SelectCategory();
}
}
HTML Page:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:GridView ID="Grd_Threshold" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="Both" AutoGenerateColumns="false" EmptyDataText="No Records." PageSize="8"
AllowPaging="true" OnPageIndexChanging="Grd_Threshold_PageIndexChanging" OnRowCommand="Grd_Threshold_RowCommand"
DataKeyNames="ID" AutoGenerateEditButton="true" OnRowCancelingEdit="Grd_Threshold_RowCancelingEdit" OnRowEditing="Grd_Threshold_RowEditing"
OnRowUpdating="Grd_Threshold_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="No.">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CLNAME") + ", " + Eval("CFNAME") +" " +Eval("CMNAME")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Doctor" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("DOCTORNAME")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Name" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("INAME")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Form" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("IFORM")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Strength" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ISTRG")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Order Date" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ORDERDATE")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" ItemStyle-Width="80px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("PRICE")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" ItemStyle-Width="80px">
<EditItemTemplate>
<asp:TextBox ID="Txt_Qty" runat="server" Width="50px" Text='<%# Eval("QTY")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("QTY")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("USERID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#E3EAEB" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
CS Code:
protected void Page_Load(object sender, EventArgs e)
{
try
{
Title = "Dashboard";
if (!Master.Page.IsPostBackEventControlRegistered)
{
BindGrid();
}
}
catch (Exception ex)
{
Common.WriteLog(ex);
}
}
private void BindGrid()
{
try
{
if (SessionUtility.DashboardRecords == null || SessionUtility.DashboardRecords.Rows.Count == 0)
{
string WebServiceUrl = SessionUtility.WebServiceUrl + "/GetAllOrders/";
JsonArrayCollection ReqResponse = Common.GetWebServiceResponse_MultipleValues(WebServiceUrl, "GET");
DataTable Patients = Common.ConvertJsonArrayObjectCollectionToDataTable(ReqResponse);
SessionUtility.DashboardRecords = Patients;
}
if (SessionUtility.UserRight != "1")
{
//Grd_Threshold.Columns[0].Visible = false;
Grd_Threshold.AutoGenerateEditButton = false;
}
else if (SessionUtility.UserRight == "1")
{
Grd_Threshold.AutoGenerateEditButton = true;
}
Grd_Threshold.DataSource = SessionUtility.DashboardRecords;
Grd_Threshold.DataBind();
UpdatePanel1.Update();
}
catch (Exception ex)
{
Common.WriteLog(ex);
}
}
Hi All,
In above written code only EDIT event is being fired & that is only once after that UPDATE,CANCEL no event is being fired. What am i doing wrong?All I want to do is change the GridRow Color on the basis of the value entered in QTY field.
I figured out the problem. The event was not firing because of the same IDs that i had given to the Label in each row. Hope this helpful to someone.
Not sure about your use of IsPostBackEventControlRegistered - I havent seen it used before but it seems that probably you are wiping out your update event by DataBind() every time in Page_Load().
Page_Load is fired every time the page loads which is then changing what the page is doing by resetting the grid.
Try wrapping the control in:
if(!IsPostBack)
{
// bind
}
so that it only does this on the first page load.