Delete link button work after page refresh in gridview - c#

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

Related

Passing data to another page for bookmarking function

I have 1 gridview look like this:
Anytime user click on Bookmark button, I want to send the data in ProgramID column of that row to the List and pass it to second gridview in another page.But my second gridview doesn't display any data. What am I doing wrong?
This is my code for Bookmark button:
protected void btnSelect_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
GridViewRow row = (GridViewRow)b.NamingContainer;
var ProgramID = row.FindControl("lblProgramID") as Label;
string stringProgramID = ProgramID.Text;
List<string> bookmarkPrograms = new List<string>();
bookmarkPrograms.Add(stringProgramID);
Session["BookmarkProgram"] = bookmarkPrograms;
}
And here is the code in Bookmark page:
protected void Page_Load(object sender, EventArgs e)
{
List<string> bookMarkPrograms = (List<string>)Session["BookMarkPrograms"];
GridView1.DataSource = bookMarkPrograms;
GridView1.DataBind();
}

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.

gridview button

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

RowCommand not getting fired on Dynamically created GridView

I am creating a gridview dynamically with a ButtonField and several BoundFields. ButtonField button type is LinkButton. If i run it the buttonclick triggers a post back but rowCommand is not triggered. It is not triggered even if i use AutogenerateSelectButton. The event is dyanamically bound. Code As follows:
protected void B_Search_Click(object sender, EventArgs e) //Search buttonclick that creates and displays the gridview
{
gd = getGridView(); //defines the gridview with columns and buttonfield
gd.DataSource = executeAdvanceSearch(); //retrieves data from DB as Dataset
gd.DataBind();
gd.RowCommand += new GridViewCommandEventHandler(gdView_RowCommand); //Rowcommand event binding
PlaceHolder1.Controls.Add(gd);
}
protected void gdView_RowCommand(object sender, GridViewCommandEventArgs e) //not getting triggered on postback
{
int index = Convert.ToInt32(e.CommandArgument);
GridView gdView = (GridView)sender;
if (e.CommandName == "IDClick")
{
//Do something
}
}
private GridView getGridView()
{
GridView gdView = new GridView();
gdView.AutoGenerateColumns = false;
gdView.AutoGenerateSelectButton = true;
string name;
string[] field = ZGP.BLL.Search.getResultFormat(Convert.ToInt32(DDL_ResultView.SelectedValue), out name); //Ignore. This jst gets columnNames
if (field.Count() != 0)
{
gdView.Columns.Add(getSelectButton()); //Adds linkbutton
foreach (string cName in field) //ignore. This adds columns.
if (!String.IsNullOrEmpty(cName))
{
gdView.Columns.Add(GV_DataColumn.getGridViewColumn((DataColumnName)Enum.Parse(typeof(DataColumnName), cName))); //Ignore. adds columns
}
}
return gdView;
}
private ButtonField getSelectButton()
{
ButtonField _bf = new ButtonField();
_bf.ButtonType = ButtonType.Link;
_bf.HeaderText = "ID";
_bf.DataTextField = "ID";
_bf.CommandName = "IDClick";
return _bf;
}
Thanks for the help.
if (e.CommandName=="CommandName")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
string boundFieldText= row.Cells[0].Text;
}
You'll need to create the grid view on each postback.

LinkButton on GridView - after selected

I asked the next question:
Get current GridView column value
And i got the right answer. now i want - that after click on the linkbutton that i have there - the text of the button will change to: "done" or its visble will be false.
how can id to that?
if referencing to the same answer, you can do something like
protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
int selectedRowIndex = Convert.ToInt32(e.CommandArgument);
var row = Gv.Rows[selectedRowIndex ];
var btn = row.FindControl("LinkButton1") as LinkButton;
if(btn != null)
{
btn.visible = false;
}
}
In RowCommand event handler,
LinkButton button=e.CommandSource as LinkButton;
button.Text="Done";
on click event of linkbutten...
protected void lnkDownload_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = (LinkButton)sender;
lnkbtn.Text = "Done";
}

Categories