get gridview label field while clicking on linkbutton - c#

I want to retrieve the value of label field by clicking on link button. it has to be fired only in onclick event only. i have tried with this , but it is giving null value.
protected void verifycount_Click(object sender, EventArgs e)
{
GridViewRow link = ((LinkButton)sender).NamingContainer as GridViewRow;
Label qrcode = (Label)link.FindControl("lblqrcode");
string result=qrcode.text;
}
pls help do solve this

In your link button in grid view column as a command argument send the value the lable in that field.think you have
<asp:BoundField DataField="FileName" HeaderText="Attached Files" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("FileName") %>'
runat = "server" OnClick = "DeleteHWAttachment" />
</ItemTemplate>
</asp:TemplateField>
Then your code behind is like that,
protected void DeleteHWAttachment(object sender, EventArgs e)
{
string filename = (sender as LinkButton).CommandArgument;
}

protected void verifycount_Click(object sender, EventArgs e)
{
LinkButton Lnk = (LinkButton)sender;
string result=Lnk.Text;
}

protected void verifycount_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
string qrcode = grdrow.Cells[0].Text;
}
Make sure that all fields are bound fields except linkbutton

Related

disabling imagebutton inside itemtemplate

How do i disable/hide a button inside that is inside a itemtemplate? i want to hide the save button on load, and then show it and hide the edit button when the edit button is clicked.
The template:
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonEdit" runat="server" CommandName="Edit" ImageUrl="loginstyling/images/Edit.png" Visible="true" />
<asp:ImageButton ID="ImageButtonUpdate" runat="server" CommandName="Update" ImageUrl="loginstyling/images/Save.png" Visible="true" OnClick="ImageButtonUpdate_Click" />
<asp:ImageButton ID="ImageButtonDelete" runat="server" CommandName="Delete" ImageUrl="loginstyling/images/Remove.png"/>
</ItemTemplate>
</asp:TemplateField>
Behind:
private void ImageButtonUpdate_Click(object sender, EventArgs e)
{
ImageButtonUpdate.Enabled = false; // not working, dosnt find the button
}
use the sender of the event
protected void ImageButtonUpdate_Click(object sender, EventArgs e){
ImageButton btnupdate= sender as ImageButton;
btnupdate.Enabled = false;
//if you need to get other controls in the same row
GridViewRow row = (GridViewRow)btnupdate.NamingContainer;
var btnedit= (ImageButton)row.FindControl("ImageButtonEdit");
btnedit.Enabled = false; // do enable or disable as you need
}
You Need to retrieve the ImageButton as follows as it is residing in the GridView
private void ImageButtonUpdate_Click(object sender, EventArgs e)
{
//Get the ImageButton that raised the event
ImageButton btn = (ImageButton )sender;
btn .Enabled = false;
}
Edit
Step 1
Provide same Onclick event for Update and Edit i.e
OnClick="ImageButtonUpdate_Click"
Step 2
Use CommandName Attribute
ImageButton btn = (ImageButton )sender;
if(btn.CommandName =="Update")
{
btn.Enabled = false;
}
if(btn.CommandName =="Edit")
{
btn.Enabled = false;
}

Changing ImageUrl onclick ImageButton Programmatically C#

I'm trying to change ImageUrl onclick Imagebutton that is present on my Repeater. I have to change it from behind code(cs file) not using jquery or js.
<asp:ImageButton ID="imgLike" ImageUrl="Content/images/thumbsup.png" onclick="imgLike_Click" runat="server" style="width:25px;height:25px" />
and here is code
protected void imgLike_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgLike = (ImageButton)rptinserting.FindControl("imgLike");
imgLike.ImageUrl = "Content/images/thumbsup1.png";
}
You cast the sender back to an ImageButton and change the ImageUrl.
protected void imgLike_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgLike = sender as ImageButton;
imgLike.ImageUrl = "/Content/images/thumbsup1.png";
}

get value from onserverclick in c#

how to get value from row in gridview using button to serverclick ?
this is my code :
<dx:ASPxGridView ID="gvwListApprover" runat="server" Width="460px">
<Columns>
<dx:GridViewDataTextColumn Caption="No" FieldName="Sequential" Width="20px"></dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="KodePosition" FieldName="KodePosition" Width="175px"></dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Jabatan" FieldName="NamaPosition" Width="175px"></dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Nama" FieldName="UserLogin" Width="265px"></dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn ReadOnly="True" Caption="Action">
<DataItemTemplate>
<input type="button" id="btnDelApp" onserverclick="btnDelApp_ServerClick('<%#Eval("KodePosition")%>');" value="Delete" runat="server" class="lookupstyle" />
</DataItemTemplate>
</dx:GridViewDataTextColumn>
</Columns>
</dx:ASPxGridView>
from onserverclick, i want get value to codebehind.
protected void btnDelApp_ServerClick(object sender, EventArgs e)
{
//how to get value from row in gridview event button on serverclick ?
}
please corrected and give me solution.
thanks
U Can go with the RowDataBoundEvent of the click.
DO Not Add any explict event for the control.
The GridView Automatically detects the event for the control clicked within it.
protected void grdClickDoubleClick_RowDataBound(object sender, GridViewRowEventArgs e) //formatted the c# code
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
//*******************************************************8
// code here. (This code given below)
//
}
}
for single click event assign
LinkButton _singleClickButton = row.FindControl("lnkBtnClk") asLinkButton;
string _jsSingleClick = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
e.Row.Attributes.Add("onclick", _jsSingleClick);
for double click event assign
LinkButton _dblClickButton = row.FindControl("lnkBtnDblClk") asLinkButton;
string _jsDoubleClick = ClientScript.GetPostBackClientHyperlink(_dblClickButton, "");
e.Row.Attributes.Add("ondblclick", _jsDoubleClick);
This assigned events we can handle in row command method as below.
protected void grdClickDoubleClick_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
switch (e.CommandName)
{
case"clk": //"clk" is command name of linkButton Row click event handler
grdClickDoubleClick.SelectedIndex = row.RowIndex;
break;
case"dblClk"://"dblClk" is command name of linkButton Row Double click event handler.
row.BackColor = System.Drawing.Color.Pink;
break;
}
}
If we want to handle both click and double click then we have to change the above click handler a bit as below
LinkButton _singleClickButton = row.FindControl("lnkBtnClk") asLinkButton;
string _jsSingleClick = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
///We make the script as below format.
//javascript:setTimeout("POSTBACK SCRIPT", 500)
_jsSingleClick = _jsSingleClick.Insert(11, "setTimeout(\"");
_jsSingleClick += "\", 500)";
e.Row.Attributes.Add("onclick", _jsSingleClick);
then we can handle both row click and double click.

How to get Text property of TextBox after ItemCommand event

I have a TextBox control inside a panel and this panel is inside DataList ItemTemplate.
After firing the ItemCommand event, everything works fine except that the TextBox.Text property is always an empty string "" although there is some text in it.
I tried several ways but without success. I would really appreciate if someone can assist me with this. Simplified code is shown below.
Thank you!
ASPX page:
<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
<ItemTemplate>
<asp:Panel ID="pnlReply" runat="server" Visible="False">
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
<asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
</asp:Panel><br />
<asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
ASPX.CS Page code behind
protected void Page_Load(object sender, EventArgs e)
{
FillDataList();
}
private void FillDataList()
{
List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
list.Add("Third");
dlDataList.DataSource = list;
dlDataList.DataBind();
}
protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "OpenPanel")
{
Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
pnlReply.Visible = true;
}
if (e.CommandName == "Send")
{
TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
//I tried this way also..
//TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
Label1.Text = txtTextBox.Text;
}
}
Please use IsPostBack in page load event. Without it your FillDataList(); is executing on every postback and resetting your DataList.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDataList();
}
}

Row index of LinkButton in GridView

I am using Link button as Template Field in GridView.
Now i want to display the row index of the Linkbutton clicked.
Please suggest me a solution Thanks in advance
suppose in Item Template this is ur link button
<ItemTemplate>
<asp:LinkButton ID="lnkapprove" Font-Underline="true" runat="server" Text="Approve" OnClick="lnkapprove_Click"></asp:LinkButton>
</ItemTemplate>
in Code Behind:
protected void lnkapprove_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
int i = Convert.ToInt32(row.RowIndex);
}
you can get row.RowIndex like this..
Hope this helps..
Please try this one:
protected void userGridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow rowSelect = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int rowindex = rowSelect.RowIndex;
}
You can also try this code in row bound event
GridViewRow rowSelect = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int rowindex = rowSelect.RowIndex;
like this
protected void userGridview_RowBound(object sender, GridViewCommandEventArgs e)
{
GridViewRow rowSelect = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int rowindex = rowSelect.RowIndex;
}
Use Container.DisplayIndex inside Page's gridview template. On code behind RowCommand handler, you'll have the row index as argument.
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:LinkButton ID="lbExample" runat="server" CommandName="DoAction" CommandArgument='<%# Container.DisplayIndex %>' ToolTip="Action link" CssClass="btn btn-sm btn-light"><i class="fa fa-edit"></i></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Categories

Resources