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";
}
Related
My file uploader allows multiple selection when i insert data in data base and that inserted data is displayed in gridview how can i make that file uploader multiple selection false when editing the records in gridview
aspx file upload code-
<asp:FileUpload ID="fil1" runat="server" multiple="multiple" />
.cs code
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
ImageButton img = sender as ImageButton;
GridViewRow grv = img.Parent as GridViewRow;
string[] textboxValues = Request.Form.GetValues("DynamicTextBox"); //Request.Form["txtSpeciality"];
JavaScriptSerializer serializer = new JavaScriptSerializer();
this.Values = serializer.Serialize(textboxValues);
if (e.CommandName == "mybutton")
{
GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer;
int rowIndex = gvr.RowIndex;
Label Itemid = (Label)GridView1.Rows[rowIndex].FindControl("lblID");
ID1 = (Itemid).Text;
Session["ID"] = ID1;
EditSubjectItem();
string Role = string.Empty;
FileUpload fil1 = grv.FindControl("fil1") as FileUpload;
fil1.AllowMultiple = false;
}
//added
}
Use this code in row editing event:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
FileUpload fil1 = GridView1.Rows[e.NewEditIndex].FindControl("fil1") as FileUpload;
fil1.AllowMultiple = false;
}
Or, use in row command event:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
ImageButton img = sender as ImageButton;
GridViewRow grv = img.Parent as GridViewRow;
if (e.CommandName == "mybutton")
{
FileUpload fil1 = grv.FindControl("fil1") as FileUpload;
fil1.AllowMultiple = false;
}
}
Edited:
Try this getting from your GridViewRow:
FileUpload fil1 = (FileUpload)GridView1.Rows[rowIndex].FindControl("fil1");
Whenever DropDownList SelectedIndexChanged, I am adding LinkButtons as ul-li list in codebehind. Each linkbuttons were assigned with IDs and a common Click event. Problem is code in Click event is not executed or maybe event is not triggered. My code below: [Edit] I tried like this as suggested in other posts (dynamically created list of link buttons, link buttons not posting back)
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
populate();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
populate();
}
void populate()
{
HtmlGenericControl ulList = new HtmlGenericControl("ul");
panel.Controls.Add(ulList);
foreach (DataRow dr in drc)
{
HtmlGenericControl liList = new HtmlGenericControl("li");
ulList.Controls.Add(liList);
var lnk = new LinkButton();
lnk.ID = dr["col1"].ToString();
lnk.Text = dr["col1"].ToString();
lnk.Click += Clicked;
liList.Controls.Add(lnk);
}
}
private void Clicked(object sender, EventArgs e)
{
var btn = (LinkButton)sender;
label1.Text = btn.ID.ToString();
}
Im missing something. Any help please.
Here the issue is with the ViewState. When the selected index of the dropdownlist changes there is a postback which takes place and the previous state is lost, so at this point you have to maintain the state of the controls.
Now in your code actually the state of the control is lost and so the click event does not fire. So the solution is to maintain the state of the controls.
The Below is a working example, you can just paste it and try.
This is my page load.
protected void Page_Load(object sender, EventArgs e)
{
for (var i = 0; i < LinkButtonNumber; i++)
AddLinkButton(i);
}
Similarly you have to maintain the state of the previously added control like this.
private int LinkButtonNumber
{
get
{
var number = ViewState["linkButtonNumber"];
return (number == null) ? 0 : (int)number;
}
set
{
ViewState["linkButtonNumber"] = value;
}
}
The below is my SelectedIndexChanged Event for the DropDownList
protected void Example_SelectedIndexChanged(object sender, EventArgs e)
{
AddLinkButton(LinkButtonNumber);
LinkButtonNumber++;
}
I have a function which dynamically creates the controls, which is called on the page load and on SelectedIndexChanged.
private void AddLinkButton(int index)
{
LinkButton linkbutton = new LinkButton { ID = string.Concat("txtDomain", index) };
linkbutton.ClientIDMode = ClientIDMode.Static;
linkbutton.Text = "Link Button ";
linkbutton.Click += linkbutton_Click;
PanelDomain.Controls.Add(linkbutton);
PanelDomain.Controls.Add(new LiteralControl("<br />"));
}
And this is the Click event for the LinkButton
void linkbutton_Click(object sender, EventArgs e)
{
//You logic here
}
I solved it using brute code lol.
Since controls' event were not bound on postback then we recreate them on postback. So in my Page_Load I called the module that re-creates the controls, thus binding them to corresponding event. This works, but...
Re-creating these controls create duplicates (Multiple Controls with same ID were found) and you will get into trouble in instances of finding a control by ID like using panel.FindControl.
To remedy this scenario, I put a check if same control ID already existed before recreating them, and voila! It works.
protected void Page_Load(object sender, EventArgs e)
{
populate();
}
void populate()
{
HtmlGenericControl ulList = new HtmlGenericControl("ul");
panel.Controls.Add(ulList);
foreach (DataRow dr in drc)
{
HtmlGenericControl liList = new HtmlGenericControl("li");
ulList.Controls.Add(liList);
if (liList.FindControl(dr["col1"].ToString()) == null)
{
var lnk = new LinkButton();
lnk.ID = dr["col1"].ToString();
lnk.Text = dr["col1"].ToString();
lnk.Click += Clicked;
liList.Controls.Add(lnk);
}
}
}
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)
{
}
I have a webpage where I have a gridview. I have populated the gridview on page load event.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadGridView();
}
}
This is the load gridview method.
private void loadGridView()
{
dataTable dt = getData(); // this function populates the data table fine.
gridView1.dataSource = dt;
gridview1.dataBind();
}
Now I have added linkButtons in one of the gridview columns in the RowDataBound event of the grid view.
protected void gvTicketStatus_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lb = new LinkButton();
lb.Text = str1; // some text I am setting here
lb.ID = str2; // some text I am setting here
lb.Click += new EventHandler(lbStatus_click);
e.Row.Cells[3].Controls.Add(lb);
}
Finally This is the event Handler code for the link button click event.
private void lbStatus_click(object sender, EventArgs e)
{
string str = ((Control)sender).ID;
// next do something with this string
}
The problem is, the LinkButtons appear in the data grid fine, but the click event does not get execute. the control never reaches the event handler code. when I click the link button, the page simply gets refreshed. What could be the problem?
I have tried calling the loadGridView() method from outside the (!isPostBack) scope, but it did not help!
Try to work with the "Command" property instead of Click event
LinkButton lnkStatus = new LinkButton();
lnkStatus.ID = string.Format("lnkStatus_{0}", value);
lnkStatus.Text = "some text here";
lnkStatus.CommandArgument = "value";
lnkStatus.CommandName = "COMMANDNAME";
lnkStatus.Command += new CommandEventHandler(lnkStatus_Command);
Otherwise, if my proposal doesn't satisfy you, you have to remove the !Postback on Page_Load event.
you have to use OnRowCommand event of the GridView.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("LinkButton1")) //call the CommandArgument name here.
{
//code
}
}
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
}
}