gridview button - c#

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);
}
}

Related

How to check a condition only once on datalist DataList.ItemDataBound Event

I have a Datalist that has a delete button in it , I want to check if the user is admin, shows this button for each item and if not dont show that. This is my ItemDataBound, my problem is that for each item it connects to database and check if user is admin or not and I am worried about getting slow for large number of items. How can I fix this problem?
protected void GroupMembersDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton deletButton = (LinkButton)e.Item.FindControl("DeletFromGroup");
// here first connecet to database to get group name
int GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
// here connect to database and find groups admin ID and checks that are the same or not ?
BusinessLayer.Group_Table GObject = new BusinessLayer.Group_Table(GroupID);
if (Convert.ToInt32(Session["ID"].ToString()) == GObject.Id)
{
deletButton.Visible = true;
}
else
{
deletButton.Visible = false;
}
}
}
Edit solution:
private BusinessLayer.Group_Table GObject;
private int GroupID;
protected void Page_Load(object sender, EventArgs e)
{
try
{
int ID = Convert.ToInt32(Session["ID"].ToString());
GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
GObject = new BusinessLayer.Group_Table(GroupID);
}
catch( Exception ee )
{
Response.Redirect("~/Home.aspx");
}
if (!IsPostBack)
{
getdata();
}
}
You could initialize a field in the class in Page_Load or the DataList's DataBinding event. Then you can access this field from ÌtemDataBound.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
GObject = new BusinessLayer.Group_Table(GroupID);
DataBindDataList(); // code that calls DataBind
}
}
private BusinessLayer.Group_Table GObject;
Another way is to store this value in the Session.

ASP.Net Gridview not rebind data on button click

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.

Delete link button work after page refresh in gridview

Here is my code for grid row delete using link button but after click its deleted data after page refresh I want delete data on the page without refresh the page i also put update panel in my grid here is my code
protected void gvContent_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="modify")
{
GridViewRow row =
(GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
// this find the index of row:
int RowIndex = row.RowIndex;
//this store the value in varName1:
int id = Convert.ToInt32(
((Label)row.FindControl("lblContentId")).Text.ToString());
Response.Redirect("ContentManage.aspx?ContentId=" +Convert.ToInt32(id));
}
if (e.CommandName == "delete")
{
GridViewRow row = (GridViewRow)
(((LinkButton)e.CommandSource).NamingContainer);
// this finds the index of row:
int RowIndex = row.RowIndex;
//this stores the value in varName1:
int id = Convert.ToInt32(
((Label)row.FindControl("lblContentId")).Text.ToString());
Content_Data.DeleteContentDetails(id);
BindGrid();
UpdatePanel1.Update();
}
You would need to register gvContentas an async post-back control using the followin
void Page_Load()
{
if (!IsPostBack)
{
ScriptManager1.RegisterAsyncPostBackControl(gvContent);
}
}
This will cause your Grid to do async post-backs.
Hope that helps
Try adding an empty RowDeleting event
protected void gvContent_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}

Create session in gridview cell

i have a gridview with tempaltefield buttons,
i want to create a session with value of a cell in selected button row ,
can anyone help me i tryed this but didnt work:
protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
Session["mysession"] = GridView1.SelectedRow.Cells[1].Text;
}
First of all, if it's just a imagebutton in a templatefield, actually you don't select de row. This line will problably throw an exception because SelectedRow is null.
But if you are using a command to select, that's correct. Maybe your event (ImageButton1_Click1) is not assigned to your image (OnClick).
You can try something like this:
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Add and event RowDataBound
grvGrid.RowDataBound += new GridViewRowEventHandler(grvGrid_RowDataBound);
}
catch
{
//...throw
}
}
protected void grvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
//...
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add an ImageButton foreach row in GridView
ImageButton ibtImageAlt = new ImageButton();
ibtImageAlt.ImageUrl = "App_Images/y.gif";
//ImageButton's ID will be the index of the row
ibtImageAlt.ID = e.Row.RowIndex.ToString();
ibtImageAlt.ForeColor = System.Drawing.Color.White;
ibtImageAlt.Font.Overline = false;
ibtImageAlt.Click += ibtImageAlt_Click;
}
}
catch
{
//...throw
}
}
protected void ibtImageAlt_Click(object sender, EventArgs e)
{
try
{
//Catch the ImageButton ID and the row in GridView
//An example to catch the value of the row selected by the ImageButton
Int32 intIndexRow = Convert.ToInt32(((ImageButton)sender).ID);
String strTest = grvGrid.Rows[intIndexRow].Cells[0].Text;
}
catch
{
//...throw
}
}

Binding dropdown list in a gridview edit item template

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
}
}

Categories