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.
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 am extracting data from database in gridview, where I have given a link button "Edit" for editing the data of that row. However while editing , one of the columns(YEAR) of my gridview becomes a dropdown list. While fetching the data from database, the Column "Year" consists of label and in edit mode it gets changed to dropdownlist. Moreover I added a condition to make the link buttons appear/disappear based on the value of the Label "Year". When I edit the particular row it throws an error because as soon as edit command is called it converts the column into dropdownlist however the row_databound searches for label. It shows "Object not set to an instance....." PLease help
Aspx
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource2" OnRowDataBound="gv1_RowDataBound" OnRowUpdating="gv1_RowUpdating" DataKeyNames="tid" CssClass="auto-style1" Width="694px">
<Columns>
<asp:TemplateField HeaderText="Sr No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="tid" SortExpression="tid" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("tid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" SortExpression="title">
<EditItemTemplate>
<asp:TextBox ID="txtTitle1" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Bind("title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="category">
<ItemTemplate>
<asp:Label ID="lblCategory" runat="server" Text='<%# Bind("category") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="rbtnlist" runat="server"><asp:ListItem Text="Soft Skills"></asp:ListItem><asp:ListItem Text="Technical Skills"></asp:ListItem></asp:RadioButtonList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Announced at" SortExpression="dt">
<ItemTemplate>
<asp:Label ID="lblDt" runat="server" Text='<%# Bind("dt", "{0:dd/MMM/yyyy HH:mm tt}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Year" SortExpression="year">
<EditItemTemplate>
<asp:DropDownList ID="ddlyr" runat="server"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblyr" runat="server" Text='<%# Bind("year") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Id" SortExpression="userid">
<ItemTemplate>
<asp:Label ID="lblUid" runat="server" Text='<%# Bind("userid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="btndel" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClientClick="return isConfirm()" Visible="false"></asp:LinkButton>
<%--<asp:LinkButton ID="btnmail" runat="server" CausesValidation="False" OnClientClick="return isConfirm()" Text="Send Mail" Visible="false"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="btnupdte" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="btncncl" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" Visible="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnmail" runat="server" CausesValidation="False" OnClientClick="return isConfirm()" Text="Send Mail" Visible="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle ForeColor="#003399" HorizontalAlign="Left" BackColor="#99CCCC" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
c#
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbly = (Label)e.Row.FindControl("lblyr");
if (lbly.Text == DateTime.Now.Year.ToString() || lbly.Text == ((DateTime.Now.Year)-1).ToString())
{
LinkButton btedt = (LinkButton)e.Row.FindControl("btnedit");
LinkButton btdel = (LinkButton)e.Row.FindControl("btndel");
LinkButton btsm = (LinkButton)e.Row.FindControl("btnmail");
btdel.Visible = true;
btedt.Visible = true;
btsm.Visible = true;
}
if ((e.Row.RowState & DataControlRowState.Edit) > 0) {
RadioButtonList rbtnlist = (RadioButtonList)e.Row.FindControl("rbtnlist");
DropDownList ddlist = (DropDownList)e.Row.FindControl("ddlyr");
for (int i = (DateTime.Now.Year); i >= ((DateTime.Now.Year)-1) ; i--)
{
ddlist.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlist.DataBind();
rbtnlist.DataBind();
}
}
}
I am assuming that you are binding your grid with a DataTable with colum name "Year". Try below code where dt refers to your Datatable:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int rowno = e.Row.DataItemIndex;
//Suppose dt is DataTable to which you are binding your grid
//and year is the column name within this datatable
string year = Convert.ToString(dt.Rows[e.Row.DataItemIndex]["year"]);
if (year == DateTime.Now.Year.ToString() || year == ((DateTime.Now.Year) - 1).ToString())
{
LinkButton btedt = (LinkButton)e.Row.FindControl("btnedit");
LinkButton btdel = (LinkButton)e.Row.FindControl("btndel");
LinkButton btsm = (LinkButton)e.Row.FindControl("btnmail");
btdel.Visible = true;
btedt.Visible = true;
btsm.Visible = true;
}
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
RadioButtonList rbtnlist = (RadioButtonList)e.Row.FindControl("rbtnlist");
DropDownList ddlist = (DropDownList)e.Row.FindControl("ddlyr");
for (int i = (DateTime.Now.Year); i >= ((DateTime.Now.Year) - 1); i--)
{
ddlist.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlist.DataBind();
rbtnlist.DataBind();
}
}
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;
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 !
I am trying to pass RegionProjectID column of my grid from query string when ever a user will click on the RegionProjectName field on the grid. I had made RegionProjectname as hyperlink and below you can find the Code also. But this is not working . Please suggest me or help that why it is not working. The pagin is also enabled in my grid view.
<asp:GridView ID="ResultGridView" runat="server" AutoGenerateColumns="False" ShowFooter="true"
DataKeyNames="RegionProjectID"
AllowPaging="True"
CellPadding="3"
OnPageIndexChanging="ResultGridView_PageIndexChanging"
OnRowDeleting="ResultGridView_RowDeleting"
CssClass="mGrid"
OnRowEditing="ResultGridView_RowEditing"
OnRowUpdating="ResultGridView_RowUpdating"
OnRowCancelingEdit="ResultGridView_RowCancelingEdit"
PageSize="15" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2"
OnRowCommand="ResultGridView_RowCommand" >
<Columns>
<asp:BoundField DataField="RegionProjectID" HeaderText="Region ID" InsertVisible="False"
ReadOnly="True" SortExpression="RegionProjectID" Visible="false" />
<asp:TemplateField HeaderText="Region Name" SortExpression="RegionProjectName">
<EditItemTemplate>
<asp:TextBox ID="txtRegion" Width="250px" runat="server" Text='<%# Bind("RegionProjectName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtRegion1" runat="server" Width="250px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton3" CommandName="Details" CommandArgument='<%# Eval("RegionProjectID") %>' Text='<%# Bind("RegionProjectName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
<%-- <asp:CommandField HeaderText="Select" ShowSelectButton="True" ShowHeader="True"/> --%>
</Columns>
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<%--<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />--%>
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>`
protected void ResultGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Details")
{
Server.Transfer("Default3.aspx?ID=" + e.CommandArgument.ToString());
}
if (e.CommandName.Equals("AddNew"))
{
TextBox txtRegion1 = (TextBox)ResultGridView.FooterRow.FindControl("txtRegion1");
TextBox txtNatureOFWork1 = (TextBox)ResultGridView.FooterRow.FindControl("txtNatureOFWork1");
if (txtRegion1.Text != "")
{
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO RegionAndProjectInfo(RegionProjectName, NatureOFWorkID ) Values('" + txtRegion1.Text + "', '" + ddlnatureOfWork.SelectedValue.ToString() + "')";
conn.Open();
cmd.ExecuteNonQuery();
}
FillVendorGrid();
conn.Close();
}
}
You must use
Response.Redirect("Default3.aspx?ID=" + e.CommandArgument.ToString());
instead of Server Transfer, cause Server Transfer does a different thing compared to Response.Redirect.
The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a statement such as
`Server.Transfer("Default3.aspx?ID=" + e.CommandArgument.ToString()`, True);
, the existing query string and any form variables will still be available to the page you are transferring to.
its more of a file transfer, instead of a redirect
http://msdn.microsoft.com/en-us/library/ms525800%28v=vs.90%29.aspx
<ItemTemplate>
<asp:hyperlink id="LinkButton3" navigateurl='<%# "Default3.aspx?ID=" + Eval("RegionProjectID")%>' text='<%# Bind("RegionProjectName")%>' runat="server" />
</ItemTemplate>
I had updated the itemtemplate of Regionprojectname as above and now getting query string .