Dropdown list loses value on gridview RowCommand - asp.net

I have a DropDownList in a GridView. I am filling the GridView dynamically in GridView RowCreated(). I want to get that DropDownList's value when I click the GridView's Command Button.
In the RowCommand function, I am trying to get the value. However, I can't find the control in that function. Surprisingly, another DropDownList works fine in the same function.
C# code follows:
if (Request.Params["ID"] != null && Request.Params["ID"] != "")
{
FirmID = Convert.ToInt32(Request.Params["ID"]);
//OfficeList = IFARecord.GetOffices(FirmID);
}
if (!IsPostBack)
{
GV_SABAdvisers.DataSourceID = "objectDataSourceSAV";
GV_SABAdvisers.DataBind();
}
protected void GV_SABAdvisers_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Index")
{
int Index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GV_SABAdvisers.Rows[Index];
//Not working
DropDownList Offices = row.FindControl("ddlLocation") as DropDownList;
String officeID = (Offices is DropDownList) ? Offices.SelectedValue : null;
DropDownList ddlJobTitle = row.FindControl("ddlJobTitle") as DropDownList;
if (ddlJobTitle is DropDownList)
{
newAdviserJobTitle.JobTitleID = Convert.ToInt32(ddlJobTitle.SelectedValue);// this works fine.
}
}
}
protected void GV_SABAdvisers_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlLocation = e.Row.FindControl("ddlLocation") as DropDownList;
DropDownList ddlJobTitle = e.Row.FindControl("ddlJobTitle") as DropDownList;
DataTable OfficeList = GetOffices(FirmID);
DataTable dtJob = MyTouchstone.Advisers.AdviserFirmJobTitle.AllJobTitle();
foreach (DataRow aOffice in OfficeList.Rows)
{
ddlLocation.Items.Add(new ListItem(aOffice["Address1"].ToString() + ", " + aOffice["Postcode"].ToString(), aOffice["OfficeID"].ToString()));
}
foreach (DataRow aJob in dtJob.Rows)
{
ddlJobTitle.Items.Add(new ListItem(aJob["JobTitle"].ToString(), aJob["JobTitleID"].ToString()));
}
}
}
ASP markup:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:GridView ID="GV_SABAdvisers" AutoGenerateColumns="false" PageSize = "10"
CssClass="cssPager" AllowPaging="true" AllowSorting="true"
OnPageIndexChanging="GV_SABAdvisers_PageIndexChanging"
runat="server" onrowcommand="GV_SABAdvisers_RowCommand"
onrowcreated="GV_SABAdvisers_RowCreated">
<PagerStyle />
<Columns>
<asp:TemplateField HeaderText="Job Title">
<ItemStyle Width="80px" />
<ItemTemplate>
<asp:DropDownList ID="ddlJobTitle" Width="80px" runat="Server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemStyle Width="200px" />
<ItemTemplate>
<asp:DropDownList ID="ddlLocation" Width="80px" runat="Server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Can anyone help me, please?

I don't see where have you used the CommandName "Index" in your source code.

Related

Trigger SelectedIndexChanged event ASP.NET

I have a DropDownList inside a GridView. I want to make some updates on database inside the SelectedIndexChanged it's not being triggered.
PS: I tried all the solutions I found over internet but always the same problem.
Here is code:
<asp:TemplateField HeaderText="Accepté" ControlStyle-Width="90px">
<ItemTemplate >
<asp:Label ID="lblState" runat="server" Text='<%# Eval("StateCode") %>' Visible = "False" />
<asp:DropDownList ID="ddlState" runat="server" Visible = "False" OnSelectedIndexChanged="ddlState_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField >
And the code behind
protected void grwConsent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text != " ")
{
DropDownList ddlState = e.Row.FindControl("ddlState") as DropDownList;
if (null != ddlState)
{
ddlState.Visible = true;
string stateValue = (e.Row.FindControl("lblState") as Label).Text;
ConsentHelper.LoadConsentStatesInList(ddlState,"-----",stateValue);
ddlState.Items.RemoveAt(0);
ddlState.Items.FindByText(stateValue).Selected = true;
}
}
}
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
//Database Updates
}
Thank you!
Try adding these properties to you DropDownList:
ViewStateMode="Enabled"
EnableViewState="true"
AutoPostBack="true"
<asp:DropDownList ID="ddlState" AutoPostBack="true" EnableViewState="true"
ViewStateMode="Enabled" runat="server" ...

Get clicked cell in gridview asp.net

I got gridview that I fill with data from my database. I would like to catch an event of click and to get attributes of the cell row and column.
all I found is row click that I can get the row clicked.
also, if there is other component to deploy the data and can get me the cell click event is good enough for me.
HTML:
<div class="grid">
<asp:GridView ID="HistoricGrid" runat="server"></asp:GridView>
</div>
C#
DBconnection db = new DBconnection();
string strtable = "select * from StudentVisit";
DataTable dt = db.ReadDataTable(strtable);
HistoricGrid.DataSource = dt; HistoricGrid.DataBind();
thanks for the help,
Moshe
You should try this.
<asp:GridView ID="GridView1" AutoGenerateColumns="False" ShowHeader="True" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Value" DataField="ItemValue" ReadOnly="true" />
<asp:BoundField HeaderText="Text" DataField="ItemText" ReadOnly="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs file
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
LinkButton linkButton = (LinkButton)e.Row.FindControl("LinkButton1");
if (linkButton != null)
{
linkButton.Attributes.Add("onclick", "window.open('someURL'); return false;");
}
}
}
You can use below code
private void gv_Cell_Click(object sender, GridViewCellEventArgs e)
{
int cellindex=e.CellIndex;
int rowindex =e.rowindex;
var value = gv.Rows[rowindex].Cells[cellindex].value;
}

Remove TemplateField during page load or before databind

I want to remove templatefield from gridview during pageload or before databind to GridView. I have 2 data sources for retrieving data. The data retrieved from one of the data sources do not have the columns ExpireDate and ExpireDays.
So I want to delete the templatefields corresponding to ExpireDate and ExpireDays if the GridView is populated from the data source that do not have those 2 fields.
Setting the visibility to false still will have error of DataRowView doesn't contain property name ExpireDate and ExpireDays.
Markup
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="No."/>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:BoundField DataField="CourseName" HeaderText="Course Enroll" />
<asp:BoundField DataField="SubMember" HeaderText="ChildMember" />
<asp:TemplateField HeaderText="Expiry Days">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label runat="server" ID="expDsL" Text=' <%# Eval("ExpiryDays") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expiry On">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label runat="server" ID="expDeL" Text=' <%# Eval("ExpiryDate") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Function">
<ItemStyle HorizontalAlign="Center" Width="100px"></ItemStyle>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/editB.gif" ID="btnEdit" runat="server" ToolTip="Edit" CommandName="Edit" CommandArgument='<%# Eval("ID") %>' />
<asp:ImageButton ImageUrl="~/Images/delB.gif" ID="btnDelete" runat="server" ToolTip="Delete" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if(Class.ToLower() != "classAA")
{
// using naveen answer can remove the column.
var expiryDateF = ((DataControlField)GridView1.Columns.Cast<DataControlField>().Where(fid => fid.HeaderText == "Expiry Days").SingleOrDefault());
var expiryDaysF = ((DataControlField)GridView1.Columns.Cast<DataControlField>().Where(fid => fid.HeaderText == "Expiry On").SingleOrDefault());
if (expiryDateF != null)
{
GridView1.Columns.Remove(expiryDateF);
}
if (expiryDaysF != null)
{
GridView1.Columns.Remove(expiryDaysF);
}
}
if(!this.IsPostBack)
{
BindData();
}
}
protected void SaveMember(object sender, EventArgs e)
{
//getting member information and perform checking.
bool success = dbbb.AddMem(memberdetails);
if(success == true)
{
BindData();
}
else
{
//prompt fail message.
}
}
protected void BindData()
{
DataTable dt = dbbb.RetrieveList();
if(dt != null)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnDel = (ImageButton)e.Row.FindControl("btnDelete");
btnDel.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this member?')");
}
}
How could I delete the column?
Two methods to achieve this
Method 1 - Remove by Index
if(!table.Columns.Contains("CustomerID1"))
{
//seven is the column index here. ie, 8th column
CustomersGrid.Columns.RemoveAt(7);
}
Method 2 - Remove using Columns Header Text
var expiryDateField= ((DataControlField)CustomersGrid.Columns
.Cast<DataControlField>()
.Where(fld => fld.HeaderText == "Expiry Date")
.SingleOrDefault());
if(expiryDateField != null)
{
CustomersGrid.Columns.Remove(expiryDateField);
}
Please not that
CustomersGrid is the name of the asp:GridView here.
table is the DataTable that acts as the DataSource of the GridView
The code should be called before DataBind of the GridView
Attach to the rowDataBound event of the grid, there you can set it dynamically.
You have to know the datasource name of the field you want to hide,
for example the headertext for the cell is "SomeHeader", but the databoundfield name for that cell is
"SomeOtherName"
then you have to check it like this (debug through GetColumnIndexByName) and
check the value of
((BoundField)cell.ContainingField).DataField
int GetColumnIndexByName(GridViewRow row, string columnName)
{
int columnIndex = 0;
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is BoundField)
if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
break;
columnIndex++; // keep adding 1 while we don't have the correct name
}
return columnIndex;
}
remember that the code above will use a BoundField... then use it like:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = GetColumnIndexByName(e.Row, "SomeOtherName");
string columnValue = e.Row.Cells[index].Text;
}
}

Always getting checkbox as false in grid

i have checkboxes in a grid. I am trying to access them from codebehind and get the data for checked /unchecked rows.But even after the checkboxes being checked I am getting them as Checked property as false only:
Aspx:
<table width="100%">
<asp:GridView ID="grdRequestsPending" runat="server" Width="100%" AutoGenerateColumns="false"
BorderWidth="1px" BorderStyle="Solid" Style="margin-left: 0px" BorderColor="#ffcc00"
RowStyle-BorderColor="#ffcc00" RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1px"
GridLines="Both" DataKeyNames="ReqID,ApproverComments" On="grdRequestsPending_ItemDataBound" OnRowDataBound="grdRequestsPending_RowDataBound"
OnPreRender="grdRequestsPending_PreRender">
<RowStyle CssClass="dbGrid_Table_row" />
<HeaderStyle CssClass="dbGrid_Table_Header" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblSelect" Text="Select All" runat="server"></asp:Label><br />
<asp:CheckBox ID="SelectAll" onclick="javascript:checkAllBoxes(this);" TextAlign="Left"
runat="server" />
</HeaderTemplate>
<ItemStyle Width="2%" />
<ItemTemplate>
<asp:CheckBox ID="chkReq" runat="server"/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="7%" />
</asp:TemplateField>
</Columns>
But when I am checking these I am always getting them as false in code behind:
protected void UpdateVMRequestStatusByCapSupLead(int StatusId)
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("ReqId", typeof(int));
dt.Columns.Add("StatusId", typeof(int));
dt.Columns.Add("ModifiedBy", typeof(string));
dt.Columns.Add("ModifiedDate", typeof(string));
dt.Columns.Add("txtCommentSupLead", typeof(string));
foreach (GridViewRow gr in grdRequestsPending.Rows)
{
CheckBox chk = (CheckBox)gr.FindControl("chkReq");
if (chk.Checked)
{
strReqId = strReqId + grdRequestsPending.DataKeys[gr.RowIndex].Value.ToString() + ',';
TextBox txtCommentSupLead = (TextBox)gr.FindControl("txtCommentSupLead");
dt.Rows.Add(dt.NewRow());
dt.Rows[dt.Rows.Count - 1]["ReqId"] = Convert.ToInt32(grdRequestsPending.DataKeys[gr.RowIndex].Value);
dt.Rows[dt.Rows.Count - 1]["StatusId"] = StatusId;
dt.Rows[dt.Rows.Count - 1]["ModifiedBy"] = Session["UserAccentureID"].ToString();
dt.Rows[dt.Rows.Count - 1]["txtCommentSupLead"] = txtCommentSupLead.Text;
dt.Rows[dt.Rows.Count - 1].AcceptChanges();
dt.Rows[dt.Rows.Count - 1].SetModified();
}
}
I am not getting the problem.I am gettign the control also correctly..
I assume that you're always databinding your GridView and not only if(!Page.IsPostBack)....
So put this in page_load
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataBindControls(); // like GridView etc.
}
}
If you DataBind controls they will lose their changes and ViewState. Even events aren't triggered then. So you should do that only on the first load if EnableViewState="true".
Here is a simple example of populating a checkbox in a gridview and getting the values out on a button click
Default.aspx
<asp:GridView ID="gv"
runat="server"
AutoGenerateColumns="false"
OnRowDataBound="gv_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkReq" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSubmit"
runat="server"
OnClick="btnSubmit_Click"
Text="Submit" />
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Create 20 rows
gv.DataSource = Enumerable.Range(1, 20);
gv.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
var isCheckedList = new List<bool>();
for(var index = 0; index < gv.Rows.Count; ++index)
{
var chkReq = (CheckBox)gv.Rows[index].FindControl("chkReq");
isCheckedList.Add(chkReq.Checked);
}
//Look at isCheckedList to get a list of current values.
System.Diagnostics.Debugger.Break();
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var index = e.Row.RowIndex;
//Strongly Bind Controls
var chkReq = (CheckBox)e.Row.FindControl("chkReq");
chkReq.Text = "Item " + index.ToString();
}
}
Since you did not show all of your code, it could be an issue like Tim Schmelter said or it could be something else. This example provides you with the basics that are needed to retrieve a checkbox value from a grid view.

GridView Delete Not Re-Binding

I have a GridView with a delete template field:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="gvCurrentDay" CssClass="gridview" OnRowCommand="gvCurrentDay_RowCommand" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="ClientName" HeaderText="Client" />
<asp:BoundField DataField="ProjectTitle" HeaderText="Project" />
<asp:BoundField DataField="TimeToAdd" HeaderText="Time Allocated" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="imbDeleteRow" ImageUrl="~/images/icons/DeleteRed.png" CommandArgument='<%# Eval("RecordID") %>' CommandName="Delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
The code runs when the button is pressed and the database entry is removed from the database, but the GridView is not rebinding, here is the code that controls the delete:
protected void gvCurrentDay_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int RecordID = Convert.ToInt32(e.CommandArgument);
tdl objTdl = new tdl();
objTdl.DeleteEntryForDay(RecordID);
GetItineryForDay(Convert.ToDateTime(txtCalendar.Text));
lblMessages.Text = "Entry removed";
}
}
And here is the first part of the GetItineryForDay procedure:
protected void GetItineryForDay(DateTime SelectedDate)
{
gvCurrentDay.DataSource = null;
gvCurrentDay.DataBind();
tdl objTdl = new tdl();
DataTable dt = objTdl.GetUsersProjectTimeForDay(SelectedDate, Convert.ToInt32(Request.Cookies["staffid"].Value));
if (dt.Rows.Count > 0)
{
DataRow row = dt.Rows[0];
int TotalTime2 = Convert.ToInt32(row[7]);
string TotalTime = row[7].ToString() + " minutes";
gvCurrentDay.DataSource = dt;
gvCurrentDay.DataBind();
Can you see from the code any reason why the GridView is not updating? The GridView sits in an UpdatePanel.
Instead of using "Delete" as the CommandName, use something else, like "ManualDelete". "Delete" is usually trapped by RowDeleting and RowDeleted events.
Alternatively, and since "the database entry is removed from the database", you could put your rebind code in the RowDeleted event.
protected void gvCurrentDay_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
GetItineryForDay(Convert.ToDateTime(txtCalendar.Text));
lblMessages.Text = "Entry removed";
}

Resources