i can bind the dropdownlist in the edit item template. The drop down list is having null values.
protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList drpBuildServers = new DropDownList();
if (grdDevelopment.EditIndex == e.Row.RowIndex)
{
drpBuildServers = (DropDownList)e.Row.Cells[0].FindControl("ddlBuildServers");
}
}
also getting an error
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
I had problems with find control, in the end I used a little bit of recursion to find the control:
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
Then to find your control make this call:
drpBuildServers = (DropDownList) FindControlRecursive(e.Row.Cells[0], "ddlBuildServers");
protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList drpBuildServers;
drpBuildServers = e.Row.FindControl("ddlBuildServers") as DropDownList;
if (drpBuildServers != null)
// Write your code here
}
}
try http://www.codeproject.com/KB/webforms/editable_gridview_control.aspx , it could be helpful
Its a solution for me:
protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList drpBuildServers;
drpBuildServers = e.Row.FindControl("ddlBuildServers") as DropDownList;
if (drpBuildServers != null)
// Write your code here
}
}
Related
i am new in asp.net i using LINQ with asp.net on button click event my gridview not rebind data and yes gridview is into the updatepanel
'>
'>
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvClientData.Rows)
{
if (((CheckBox)gvr.FindControl("chkdisplay")).Checked == true)
{
string Index = ((Label)gvr.FindControl("lblIndex")).Text;
int GIIndex = Convert.ToInt32(Index);
GI_InsureMaster insertclientinfo = vjdb.GI_InsureMasters.Single(upd => upd.GIMastIndex == GIIndex);
insertclientinfo.SendToCompany = true;
vjdb.SubmitChanges();
}
}
BindAgencyData();
Response.Redirect(Request.RawUrl);
}
It seems you are trying to modify an object and then saving it back to the DB, but you are doing it wrong.
You are querying the object from a different Data Context, vjdb and you are calling SubmitChanges on linqobject. You should call SubmitChanges on vjdb
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvClientData.Rows)
{
if (((CheckBox)gvr.FindControl("chkdisplay")).Checked == true)
{
string Index = ((Label)gvr.FindControl("lblIndex")).Text;
int GIIndex = Convert.ToInt32(Index);
GI_InsureMaster insertclientinfo = vjdb.GI_InsureMasters.Single(upd => upd.GIMastIndex == GIIndex);
insertclientinfo.SendToCompany = true;
vjdb.SubmitChanges(); //HERE
}
}
BindAgencyData();
Response.Redirect(Request.RawUrl);
}
Assuming that BindAgencyData is querying database for latest/updated record and then binding the data to the grid.
How can I check if a column exists in result that populate a listview?
The listview is populated from a stored procedure.
This is what I tried but no success:
<%# Container.DataItem.GetType().GetProperty("Phone")==null?"phone is null":"we have phone property" #>
or should I use e instead Container.DataItem ?
First, i would use codebehind if it's getting complicated (i use it almost always). Here i would use the ListView's ItemDataBound event which is triggered for every item:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
// assuming you have an ItemTemplate with a label where you want to show this
Label lblInfo = (Label) e.Item.FindControl("LblInfo");
DataRowView rowView = (DataRowView)e.Item.DataItem;
if (rowView.Row.Table.Columns.Contains("Phone"))
{
lblInfo.Text = "we have the phone property";
}
else
{
lblInfo.Text = "no phone available";
}
}
}
That makes the code much more readable, maintainable, debuggable and type safe.
You can check this in OnItemDataBound.
protected void lstSample_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
Label lblText = null;
Boolean isColumnExists = false;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DataRowView dr = (DataRowView)e.Item.DataItem;
isColumnExists = dr.DataView.Table.Columns.Contains("Hello");
lblText = (Label)e.Item.FindControl("lbltext");
if (isColumnExists)
{
lblText.Text = dr.Row["Hello"].ToString();
}
else
{
lblText.Text = dr.Row["Movies"].ToString();
}
}
}
Hope this helps!
Also, I found a solution:
public bool CheckProperty(string property_name)
{
try
{
Eval(property_name);
}
catch { return false; }
return true;
}
I have this Module in my project in which I have 2 gridviews. One is for the Main MenuModule and the other one is for it's subMenu. I created a List so that when a row on my Main Menu Module has been checked and it has a corresponding submenu, it will show on the SubMenu Gridview.
Now, I can see my SubMenuGridview when I get back to that page (I used session), but I noticed that the checkbox I ticked were all gone.
My problem was on how can my page remember the checkboxes I checked, both from my Main Menu Module gridview and from my Submenu gridview.
protected void cbxSelect_CheckedChanged(object sender, EventArgs e)
{
SubMenuGrid.DataSource = null;
SubMenuGrid.DataBind();
Business.SubMenuModules sub = new Business.SubMenuModules();
List<oSubList> oList = new List<oSubList>();
int counter = 0;
foreach (GridViewRow nRow in gvModuleList.Rows)
{
Int32 intModID = Convert.ToInt32(nRow.Cells[0].Text);
CheckBox chkBx = (CheckBox)nRow.FindControl("cbxSelect");
if (chkBx.Checked == true)
{
counter = counter + 1;
var oModList = sub.GetAllMenuPerModuleID(intModID);
if (oModList.Count > 0)
{
foreach (var rec in oModList)
{
oSubList olist = new oSubList
{
ID = rec.ID,
ModuleID = rec.ModuleID,
Submenu = rec.Submenu,
Description = rec.Description
};
oList.Add(olist);
}
Session["list"]=oList;
SubMenuGrid.DataSource = oList;
SubMenuGrid.DataBind();
}
}
}
}
This can be done using:
ViewState
SessionPageStatePersister
Custom Session based solution
For view state-see the below posted link in comments..
Custom Session based solution
We are going to use the pre render method.
This method is being invoked after the page has been initialized but before it saved its ViewState and rendered.
Are are going to load the Request.Form into a session variable and load it back with each call to the page that is not a postback.
protected void Page_PreRender(object sender, EventArgs e)
{
if (!Page.IsPostBack && Session["PageState"] != null)
{
NameValueCollection formValues = (NameValueCollection)Session["PageState"];
String[] keysArray = formValues.AllKeys;
for (int i = 0; i < keysArray.Length; i++)
{
Control currentControl = Page.FindControl(keysArray[i]);
if (currentControl != null)
{
if (currentControl.GetType() == typeof(System.Web.UI.WebControls.TextBox)) ((TextBox)currentControl).Text = formValues[keysArray[i]];
else if (currentControl.GetType() == typeof(System.Web.UI.WebControls.CheckBox))
{
if (formValues[keysArray[i]].Equals("on")) ((CheckBox)currentControl).Checked = true;
}
else if (currentControl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
((DropDownList)currentControl).SelectedValue = formValues[keysArray[i]].Trim();
}
}
}
if(Page.IsPostBack) Session["PageState"] = Request.Form;
}
SessionPageStatePersister
The abstract PageStatePersister class represents the base class that encapsulates the Page State storage and processing.
Storage can be done with the default HiddenFieldPageStatePersister class or with SessionPageStatePersister.
When SessionPageStatePersister is used, the .NET Server manages the _VIEWSTATE in a session object instead of a hidden field on your form.
Changing the state storage looks like this:
protected override PageStatePersister PageStatePersister
{
get
{
return new SessionPageStatePersister(this);
}
}
I apologize if this is a real simple question, but I can't find anyone else to ask. I have a gridview with a button column. I'm trying to use the button column to send a filename to another page. I pulled this code off of another solution here, but I get an error: "Does not contain a definition for 'Item'" on this line: ListViewDataItem item = (ListViewDataItem)e.Item; and I have no idea which Using-namespace (is that what they are called?) to use.
protected void gvFiles_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
ListViewDataItem item = (ListViewDataItem)e.Item;
int index = item.DataItemIndex;
string fileID = ((ListView)sender).DataKeys[index]["fileID"].ToString();
Response.Redirect("irMain.aspx?#filename=" + fileID);
}
}
I suppose you are dealing with a gridView not a ListView, then the code should be
protected void gvFiles_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
int index = Convert.ToInt32(e.CommandArgument);
string fileID = ((GridView)sender).DataKeys[index]["fileID"].ToString();
Response.Redirect("irMain.aspx?#filename=" + fileID);
}
}
I'm trying to add data to a literal which place inside a gridview. Currently code look like this
protected void GvListingRowDataBound(object sender, GridViewRowEventArgs e)
{
var query = DisplayAllData();
Literal info = (Literal)e.Row.FindControl("ltritemInfo");
if(query != null)
{
foreach (var listing in query)
{
var list = DisplayListById(listing.id);
info.Text = "<h3>" + list.title + "</h3>";
info.Text += "<h4>" + list.description + "</h4>";
}
}
}
This will generate an error
Object reference not set to an instance of an object.
If anyone has an idea about this it will be great help
Thanks
Ensure you're only operating on the data rows, and not the header, footer, separator, pager, etc. The enum for this is DataControlRowtype. This is why your info object/reference is null, as it operates on the header first.
Check that the e.Row.RowType is of type DataRow.
For safety, also check that your info is not null.
protected void GvListingRowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var query = DisplayAllData();
Literal info = (Literal)e.Row.FindControl("ltritemInfo");
if(query != null && info !=null)
{
foreach (var listing in query)
{
var list = DisplayListById(listing.id);
info.Text = string.Format("<h3>{0}</h3><h4>{1}</h4>",
list.title, list.description);
}
}
}
}