get value from onserverclick in c# - 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.

Related

Check if hyperlink in gridview has been clicked c#

Hi I would like to know how to get the text field and index from a hyperlink in a gridview that has been clicked. Basically, the user would click on the hyperlink in the gridview and when the user has been navigated to the link, the text field and index of the link would be stored into an arraylist. Does anyone have any idea how I can go about doing this?
I have came up with this "pseudo code" for the onrowdatabound event handler in gridview:
ArrayList linksClicked = new ArrayList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("links");
if (hl != null)
{
linksClicked.Add(h1.ToString());
}
}
You should use ItemTemplate with LinkButton. In this button you can keep index or id like CommandArgument, also you easily catch event onClick and add index to your array. Use this sample.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="hyperLinkButton" Text="link" PostBackUrl="youruri.com" runat="server"
CommandArgument="<%# Eval("SomeFieldYouNeedArguementFrom") %>" OnClick="hyperLinkButton_Click" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void hyperLinkButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument;
// do what you need here
}

How to disable button in row databound event in gridview?

i want to disable button in row data bound . when its text or value is 'Waiting for Approval'. im getting this error . Object reference not set to an instance of an object.// button.Enabled = true;
protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Button button = (Button)e.Row.FindControl("btnReportedlink");
string Id =((DataRowView)e.Row.DataItem)["ReportLinks"].ToString();
if (Id == "Waiting for Approval")
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}
my aspx
<asp:TemplateField HeaderText="Reportd Link" ItemStyle-HorizontalAlign="center" >
<ItemTemplate>
<button onclick="window.open('<%#Eval("ReportLinks")%>', '_blank');" title='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"> Link</button>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
Why are you using the HTML <button /> element ? use <asp:button /> web server control from asp.net for a better control over reading and disabling the server controls.
Use the OnClientClick property to specify additional client-side script that executes when a Button control's Click event is raised.
<ItemTemplate>
<asp:button onclientclick="javascript:window.open('<%#Eval("ReportLinks")%>', '_blank');"
text='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"/>
</ItemTemplate>
With the above setup, now you will be able to access the button in row data bound event with NO more object references error.
use DataBinder work fine
protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Button button = (Button)e.Row.FindControl("btnReportedlink");
string Id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ReportLinks"));
if (Id == "Waiting for Approval")
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}

Confirmation dialog box on CommandField click [duplicate]

I want to prompt the user for confirmation when he tries to delete a record in a detail view? I have command filed in which showDeletebutton set to true.
I found how to do the confirmation for gridview, but how can I modify to match detail view?
Code:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// loop all data rows
foreach (DataControlFieldCell cell in e.Row.Cells)
{
// check all cells in one row
foreach (Control control in cell.Controls)
{
// Must use LinkButton here instead of ImageButton
// if you are having Links (not images) as the command button.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete")
// Add delete confirmation
button.OnClientClick = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
}
}
Anybody?
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
.....
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity"
SortExpression="Quantity" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="New" Text="New"></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView
This can be done easily on the markup code. I simply added the js code to the onClientClick property of the delete button:
OnClientClick="return confirm('Are you sure you want to delete this record');"
Or if you want do this in the code behind:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
LinkButton bttn = (LinkButton)DetailsView1.FindControl("lnkDelete");
bttn.OnClientClick = "return confirm('Are you sure you want to delete this record!');";
}
I found the answer to my question.
My answer:
protected void DViewComputer_DataBound1(object sender, EventArgs e)
{
int noRow = DViewComputer.Rows.Count - 1;//get the no of record
if (noRow >0)
{
Button button = (Button)(DViewComputer.Rows[noRow].Cells[0].Controls[2]);
// Add delete confirmation
((System.Web.UI.WebControls.Button)(button)).OnClientClick = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
Anyways thanks for your help guys.
foreach (Control control in cell.Controls)
{
// Must use LinkButton here instead of ImageButton
// if you are having Links (not images) as the command button.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete")
// Add delete confirmation
button.Attributes.Add("onclick","your javascript here");
}
Please see the below URL......
http://www.codeproject.com/Articles/32756/ASP-NET-GridView-delete-confirmation-using-asp-Com
This corrects the OP's solution. The code was translated from the code found here: http://forums.aspfree.com/net-development-11/confirm-button-when-deleting-detailsview-120113-2.html
protected void dvEvent_DataBound(object sender, EventArgs e)
{
int commandRowIndex = dvEvent.Rows.Count - 1;
if (commandRowIndex > 0)
{
DetailsViewRow commandRow = dvEvent.Rows[commandRowIndex];
DataControlFieldCell cell = (DataControlFieldCell)commandRow.Controls[0];
foreach (Control ctrl in cell.Controls)
{
if (ctrl is ImageButton)
{
ImageButton ibt = (ImageButton)ctrl;
if (ibt.CommandName == "Delete")
{
ibt.ToolTip = "Click here to Delete";
ibt.CommandName = "Delete";
ibt.Attributes["onClick"] = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
}
}
}

get gridview label field while clicking on linkbutton

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

Get Gridview RowIndex outside of the gridview

My Button1 is inside a Panel, I want to access the rowindex so that I will hide that Imagebutton. But when I enter debug mode, the GridView1.SelectedIndex has a null value. pleaase help!
protected void Button1_Click1(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (row.RowIndex == Convert.ToInt32(GridView1.SelectedIndex))
{
ImageButton StopButton = (ImageButton)row.FindControl("stopImageButton");
ImageButton StartButton = (ImageButton)row.FindControl("startImageButton");
StopButton.Visible = true;
StartButton.Visible = false;
}
}
}
this.StopTimeNotesPanel_ModalPopupExtender.Hide();
}
You said that Button is inside a panel. To be able to handle GridView events effectively, use a button inside Gridview itself.
OR if you still want to use button in panel, then,
1.) First Add a select button inside Gridview. Select a row using the select button and
2.) click the button in Panel.
GridView.SelectedIndex is set only when you have selected a row of Grid view. Two ways are possible:
1.) Set AutoGenerateSelectButton property to true.
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogenerateselectbutton="True"
runat="server">
2.) Add yourself a buttonField inside section of gridView as:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
runat="server">
<columns>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="Select Customer"
text="Select"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
</columns>
</asp:gridview>
Now after a row is selected, two events of GridView will be fired:
selectedindexchanging & selectedindexchanged.
Only when required, do this step below to get SelectedRow in SelctedIndexChangedEvent
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text; // just for Display
}
Now, inside your button click event , get the selected index:
protected void Button1_Click1(object sender, EventArgs e)
{
int i = CustomersGridView.SelectedIndex;
}
Code for for your foreach loop:
ImageButton StopButton = (ImageButton)row.FindControl("stopImageButton");
ImageButton StartButton = (ImageButton)row.FindControl("startImageButton");
StopButton.Visible = true;
StartButton.Visible = false;

Categories

Resources