Passing Gridview LinkButton ID to Modal PopUp in ASP.NET - c#

I have a button like this in my gridview,
<asp:LinkButton runat="server" ID="lnkCustDetails" Text='<%# Eval("CustomerID") %>' OnClick="lnkCustDetails_Click" />
and I get the id like this:
protected void lnkCustDetails_Click(object sender, EventArgs e)
{
LinkButton lb = sender as LinkButton;
string custID = lb.Text;
lblCustValue.Text = custID;
ModalPopupExtender1.Show();
}
My problem is, what can I use instead of having the id in Text='<%# Eval("CustomerID") %>' because this displays the id of the customer in the linbutton, when I want the link button to display "Details"

You can switch to handling commands instead of clicks:
<asp:LinkButton runat="server" ID="lnkCustDetails"
Text='Details'
CommandArgument='<%# Eval("CustomerID") %>'
OnCommand="lnkCustDetails_Click" />
And to get the id:
protected void lnkCustDetails_Click(object sender, EventArgs e)
{
LinkButton lb = sender as LinkButton;
string custID = lb.CommandArgument;
lblCustValue.Text = custID;
ModalPopupExtender1.Show();
}
One note though: I am not sure if you can handle LinkButton's command directly if it lives inside the grid view. You might need to subscribe to grid view's On Command instead - please check.

Related

how to get value of a template field label of a gridview, getting error Object reference not set to an instance of an object

While getting the value I'm getting Object reference not set to an instance of an object. how to get labeled label value in code behind. How to get the value in custom event
<asp:GridView runat="server" ID="gridviewQuoteDetails" EmptyDataText="No records Found..." AutoGenerateEditButton="false" OnRowEditing="gridviewQuoteDetails_RowEditing" OnRowUpdating="gridviewQuoteDetails_RowUpdating" DataKeyNames="id" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href='Quote.aspx?val=<%#Eval("id")%>'>
<asp:Label ID="lblid" runat="server" Text='<%#Eval ("id")%>'></asp:Label>
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" Text="Update" runat="server" OnClick="OnUpdate" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind
protected void OnUpdate(object sender, EventArgs e)
{
string qouteid = ((Label)gridviewQuoteDetails.FindControl("lblid")).Text;
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string id = (row.Cells[0].Controls[0] as TextBox).Text;
string Description = (row.Cells[1].Controls[0] as TextBox).Text;
}
Gridview is a collection of rows right, but you are trying to search the control directly within the gridview thus ((LinkButton)gridviewQuoteDetails.FindControl("lbllnknm")) will be null and when you are trying to access the Text property of LinkButton you are getting NRE error.
You need to loop through the rows of your gridview control to access each linkbutton present in each row like this:-
foreach (GridViewRow row in gridviewQuoteDetails.Rows)
{
LinkButton lbllnknm= row.FindControl("lbllnknm") as LinkButton;
}
But ideally you would not be needing this. I guess you are trying to get the value in OnRowEditing event handler. If that is the case then you can fetch the value of linkbutton like this:-
protected void gridviewQuoteDetails_RowEditing(object sender, GridViewUpdateEventArgs e)
{
string qouteid = ((Label)gridviewQuoteDetails.Rows[e.NewEditIndex]
.FindControl("lbllnknm")).Text;
}
Update 2:
As per your comment you are trying to access the LinkButton text on click of LinkButton itself which means the event is raised by LinkButton itself. You can simply use the sender object like this:-
protected void OnUpdate(object sender, EventArgs e)
{
LinkButton LinkButton2 = sender as LinkButton;
GridViewRow row = LinkButton2.NamingContainer as GridViewRow; //Get the gridview row
Label lblid = row.FindControl("lblid") as Label;
string qouteid = lblid.Text;
}

ItemList FindControl working for 1 item

I have a LinkButton within my ItemTemplate of my ListView:
<asp:ListView ID="lvNotification" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbReject" OnClick="Reject_Click" CommandArgument='<%# Eval("offerID") %>' Text="Reject" />
</ItemTemplate>
</asp:ListView>
Then in my code I have:
protected void Reject_Click(object sender, EventArgs e)
{
var lbreject = lvNotification.Items[0].FindControl("lbReject") as LinkButton;
string c = lbreject.CommandArgument;
}
The ListView retrieves 4 rows correctly, and I have placed the Eval("offerID") and it shows an offerID for each item in the list, however when I place it as the CommandArgument in the LinkButton and debug it, it shows the value 1 on ever item in the ListView, I am trying to place the offerID in each LinkButton CommandArgument and be able to access it, but I cannnot do so.
What am I doing wrong?
That is because you are always checking the first item (0 index). You can get the LinkButton from the sender and check its command argument.
protected void Reject_Click(object sender, EventArgs e)
{
var lbreject = (LinkButton)sender;
string c = lbreject.CommandArgument;
}
Here actually OnItemCommand event of ListView can be handy. This way you can control all events from the single point:
<asp:ListView ID="lvNotification" OnItemCommand="lvNotification_OnItemCommand" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbReject" CommandName="Reject_Click" CommandArgument='<%# Eval("offerID") %>' Text="Reject" />
</ItemTemplate>
</asp:ListView>
protected void lvNotification_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "Reject_Click"))//or any other event
{
LinkButton lbreject = (LinkButton) sender;
string c = lbreject.CommandArgument;
}
}

Text property overriden

I have a textbox and a linkbutton within the EditItemTemplate of my ListView:
<asp:TextBox ID="txt_notes" runat="server" Placeholder='<%# Eval("notes") %>'></asp:TextBox>
<asp:LinkButton ID="btn_update" class="btn btn-sm btn-success" OnClick="btn_update_Click" CommandArgument='<%# Eval("carID") %>' runat="server" >Update</asp:LinkButton>
I have this code:
protected void btn_update_Click(object sender, EventArgs e)
{
var btn_update = (LinkButton)sender;
int ID = System.Convert.ToInt32(btn_update.CommandArgument);
TextBox txt_notes = (TextBox)listview.EditItem.FindControl("txt_notes");
string notes = txt_notes.Text;
}
Now when I set a break point at string notes = txt_notes.Text it says the txt_notes.Text has nothing in it even though I have typed something in the textbox, so it seems that it is being overridden by the ItemDataBound or the PageLoad.
Does anyone know how I should overcome this problem?
It looks like you are binding your listview on page load but not checking if it is on a postback. try the below
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
listview.DataSource = your_data_source;
listview.DataBind();
}
}

Get the dynamically bound hidden field value inside a list view in code-behind

In my aspx page, I have,
<asp:ListView ID="listview1" runat="server" DataSourceID="dtasrc_load">
<ItemTemplate>
<h4>
<asp:Label ID="lbl_titlename" runat="server" Text='<%#Eval("abt_vch_Title") %>'></asp:Label>
</h4>
<asp:LinkButton runat="server" OnClick="Content_Load" class="btn">Edit</asp:LinkButton>
<asp:HiddenField ID="hiddenID" runat="server" Value='<%#Eval("abt_int_ID") %>' />
</ItemTemplate>
</asp:ListView>
I need to access the value in the hidden field control so that I can pass that value to the database on the linkbutton click event. Below is where I've gotten so far.
protected void Content_Load(object sender, EventArgs e)
{
HiddenField hd = new HiddenField();
HiddenField myhiddenfield = new HiddenField();
myhiddenfield = (HiddenField)listview1.FindControl("hiddenID");
int myID = Convert.ToInt32(myhiddenfield.Value);
I get a run-time error as "Object not referenced to instance of an object". The value seems to be null.
Can anyone tell me why I am getting this? What should I do?
give your linkbutton an id
<asp:LinkButton runat="server" OnClick="Content_Load" class="btn"
id="editlinkbutton">Edit</asp:LinkButton>
and change your code to this
protected void Content_Load(object sender, EventArgs e)
{
LinkButton editlinkbutton = sender as LinkButton;
HiddenField myhiddenfield = editlinkbutton.NamingContainer.FindControl("hiddenID") as HiddenField;
int myID = Convert.ToInt32(myhiddenfield.Value);
}
edit: maybe linkbutton doesnt have to have an id, not sure. my linkbuttons usually have id's :)
I recently had a similar issue. Try not to look for System.Web.UI.WebControls.HiddenField, but rather for System.Web.UI.HtmlControls.HtmlInputHidden-class, here.
Additionally, you should be more cautious, rather use
System.Web.UI.HtmlControls.HtmlInputHidden hi =
listview1.FindControl("hiddenID") as ystem.Web.UI.HtmlControls.HtmlInputHidden;
if(hi != null)
...

Adding functionality to an imageButton in a gridview

I have an ImageButton control as part of a GridView control that is displayed as an ItemTemplate and in the same GridView. I have a regular Button control to which I added some code like this
if (e.CommandName == "addToSession")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
string ISBN = selectedRow.Cells[0].Text;
string bookTitle = selectedRow.Cells[1].Text;
string image = selectedRow.Cells[2].Text;
//storing title, author, pictureUrl into session variables to 'carry them over' to RateBook.aspx
Service s = new Service();
Session["ISBN"] = ISBN;
Session["bookTitle"] = bookTitle;
Session["ImageUrl"] = s.returnImageUrl(bookTitle);
if (Session["userName"] == null)
{
Response.Redirect("registerPage.aspx");
}
else
{
Response.Redirect("RateBook.aspx");
}
}
else if (e.CommandName == "ratedBooks")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
string bookTitle = selectedRow.Cells[1].Text;
Service s = new Service();
Session["ImageUrl"] = s.returnImageUrl(bookTitle);
Response.Redirect("BookRated.aspx");
}
when I run this code I get a format exception and again I am not sure why. I have altered the image button a bit and nested the image in a link button which seems to be more correct.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="ratedBooks">
<asp:Image ID="ImageButton1" ImageUrl='<%#Eval("pictureUrl") %>' runat="server" />
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Please advise.
Regards,
Arian
I believe you can accomplish your needs with an ImageButton, as it supports all the major Button functionality, including CommandName (see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.commandname.aspx).
Try this out:
<asp:ImageButton ID="LinkButton1" runat="server"
CommandName="ratedBooks"
ImageUrl='<%#Eval("pictureUrl") %>' />
Also, note that your format exception could be coming from the lines that read:
Convert.ToInt32(e.CommandArgument);
The reason being that there appears from this code snippet to be no value assigned to the CommandArgument of the button. Convert.ToInt32 requires a valid integer value to be passed in, which means that the ImageButton needs to have a number bound to its CommandArgument property.
If you based your solution on this MSDN reference (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx), note that the <asp:ButtonField> column type gets special treatment; the CommandArgument is set to the row index. When you use a template field, ASP.NET requires you to specify or data bind your own command argument.
Update
This question contains details on binding the grid view row index to a custom button:
ASP.NET GridView RowIndex As CommandArgument
A possible solution is to add the ItemDataBound event handler and search for the image button change its image url.
MyGrid.RowDataBound += new RepeaterItemEventHandler(MyGrid_RowDataBound);
void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
ImageButton image = e.Row.FindControl("MY_IMAGE_CONTROL") as ImageButton;
image.ImageUrl = "PATH_TO_IMAGE";
}
}
Hope it helps.
You need CommandArgument
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Delete" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ImageUrl="~/Modelos/Img/deleted.gif" />
</ItemTemplate>
</asp:TemplateField>
code behind C#
public void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
string t;
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = grid1.Rows[index];
t = selectedRow.Cells[2].Text;
}
}
On ASPX
<asp:GridView ID="grid1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" ...

Categories