Make a Buttonfield Execute a javascript - c#

I want to execute a JavaScript function on the lcick of a ButtonField of a gridview
As there is no onclick or onclient click present
<asp:ButtonField HeaderText="Submit" Text="Submit" CommandName="Submit" />

Handle the RowDataBound event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Presume that the buttonField is at 1st cell
LinkButton link = e.Row.Cells[0].Controls[0] as LinkButton;
if (link != null)
{
link.Attributes.Add("onclick", "alert('Hello');");
}
}
}

You cannot work with ButtonField for this scenario, better check Javascript before asp:ButtonField click answered by Steve.
Happy Coding !!

Try below code
protected void GridView1_RowDataBound(object sender, GridViewRowCommandEventArgs e)
{
if(e.Row.RowIndex != -1)
{
//Here Index of Cell will be the No of Delete Column.
LinkButton btn = (LinkButton)e.Row.Cells[1].Controls[1];
btn.Attributes.Add("onclick","if (confirm('Are you sure to delete this admin user?')){return true} else {return false}");
}
}

in grdRewards_RowDataBound we can write
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btnbutton As LinkButton = DirectCast(e.Row.Cells(13).Controls(0), LinkButton)
btnbutton.Attributes.Add("onclick", "javascript:return ShowDialog()")

Related

ASP Radiobutton oncheckedchange not firing

I have a custom radiobutton below inside of a repeater and when a user clicks it, it is supposed to fire the code behind, however this is not firing at all. I've placed a breakpoint at the beginning of the method and it isnt ever reached. The only thing that does happen is a postback of the updatepanel
<EclipseUI:CustomRadioButton runat="server" ID="RadioButton_Select"
ClientIDMode="AutoID" ToolTip='<%# "id_" + Eval("FeaturePackId") %>' GroupName='<%# "id_" + Eval("FeaturePackId") %>'
OnCheckedChanged="RadioButton_Select_OnCheckedChanged" AutoPostBack="True"/>
The code behind is simply this, it takes the value of each checked radiobutton and places it in a hidden field for use later.
protected void RadioButton_Select_OnCheckedChanged(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Clear();
foreach (RepeaterItem repeaterItem in Repeater_Select.Items)
{
CustomRadioButton radioButton = repeaterItem.FindControl("RadioButton_Select") as CustomRadioButton;
if (radioButton != null)
{
if (radioButton.Checked)
{
sb.Append(radioButton.GroupName.Substring(4));
}
else
{
sb.Replace(radioButton.GroupName.Substring(4), "");
}
}
}
HF_Feature.Value = sb.ToString();
}
My problem is that this doesnt fire at all.
EDIT
My code is now as follows, and the item databound event is hit and so is the += assignment of the eventhandler to the radiobutton but clicking the radiobutton still doesnt hit my breakpoint in the OnCheckedChange of the radiobutton method:
protected void Page_Load(object sender, EventArgs e)
{
Repeater_Select.ItemDataBound += new RepeaterItemEventHandler(Repeater_Select_OnItemDataBound);
}
protected void Repeater_Select_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CustomRadioButton customRbtn = (CustomRadioButton)e.Item.FindControl("RadioButton_Select");
customRbtn.CheckedChanged += new EventHandler(RadioButton_Select_OnCheckedChanged);
}
}
But it is still not working
in a repeater you will have to wire up the OnCheckedChanged events inside of the repeaters _ItemDataBound.
protected void Page_Init(object sender, EventArgs e
{
myRepeater.ItemDataBound += new RepeaterItemEventHandler(myRepeater_ItemDataBound);
}
and then..
private void myRepeater_ItemDataBound(object sender, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType !=
ListItemType.AlternatingItem)
{
CustomRadioButton customRbtn = (CustomRadioButton)e.Item.FindControl("RadioButton_Select");
//Now you have an instance of your eclipse radio button so you can do what you want with it.
}
}
Have a looksy at this: OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked
at at way the "_ItemDataBound" is wired up.
and this article may be a little closer to your problem if your using item template in the .aspx file. http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065/ASPNET-Tip-Use-the-ItemDataBound-Event-of-a-Repeater.htm
Just try to setting CauseValidation="false" (besides Autopostback="true")
<EclipseUI:CustomRadioButton runat="server" ID="RadioButton_Select" ClientIDMode="AutoID" ToolTip='<%# "id_" + Eval("FeaturePackId") %>' GroupName='<%# "id_" + Eval("FeaturePackId") %>' OnCheckedChanged="RadioButton_Select_OnCheckedChanged" AutoPostBack="True" CauseValidation="false"/>
It works...

How to populate textbox with value of gridview control?

With just a regular value you would write something like
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow email = GridView1.SelectedRow;
txtbox.Text = email.Cells[5].Text;
}
However, I want to populate that textbox with a value that is a textbox control in the gridview. I have a list of rows and depending on which row I select that textbox will populate with that control value. Any help/advice would be greatly appreciated.
I would suggest using datakeys for this. It's a lot easier and a lot more reliable than using the cell index:
<asp:GridView ID="Gridview1" runat="server" DataKeyNames="Column1, Column2" ...>
Then, in the code-behind you can access the values like this:
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow email = GridView1.SelectedRow;
txtbox.Text = (string)GridView1.DataKeys[email.RowIndex]["Column1"];
}
Try this:
GridView1.RowCommand += GridView1_RowCommand;
private void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(Convert.ToString(e.CommandArgument));
GridViewRow row = GridView1.Rows[index];
this.NameTextBox.Text = Server.HtmlDecode(row.Cells[1].Text);
}
}
Try below code.
TextBox tb = GridView1.SelectedRow.FindControl("textboxId") as TextBox;
textbox.Text = tb.Text;
You can find a better code here on how to populate gridview with the text box values of your form.
http://sharepoint-2010-world.blogspot.in/2013/10/populating-grid-with-form-values.html

GridView column with checkbox field with SQL

I have GridView with 2 columns.
The first column is: test-label (TemplateField)
The second: checkbox (asp:CheckBoxField) that connect to sql table with bit column (done).
I want that on page load - the page will check every row, where the checkbox = true, the test-label.visble will be false.
I know how to write code with SELECT statement to check the value from the SQL table, but don't know how to check every row on the gridview on the page-load.
how can I do that?
(i can't use findcontroll for the checkbox because it's checkboxfield and not just "checkbox".
<asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" />
so, what can I do here? maybe to replace that field with regular cb? (i don't know how to do there databind - on the regular cb).
you can use GridView.RowDataBound Event
so you can do something like
protected void GVRowDataBound(object sender, GridViewRowEventArgs e)
{
var check = (CheckBox) e.Row.FindControl("ID"); // ID is id of the checkbox
var lable = (Label) e.Row.FindControl("LableID");
if(check != null && lable != null)
{
if(check.Checked)
{
lable.Visible = false;
}
}
}
You can't do it in Page.Load because the GridView isn't databound yet.
Try handling GridView.RowDataBound.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)e.Row.FindControl("checkbox");
Label lbl = (Label)e.Row.FindControl("test-label");
lbl.Visible = !(cb.Checked);
}
}

c# GridView RowDataBound Error

I have a gridview that should be editable when a row is clicked. This gridview is clickable in a row so when I clicked it, the row will be displayed in other pages for editing reason. I got an error like this
Specified argument was out of the range of valid values.
Parameter name: index.
This is happening for this line:
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[1].Controls[1];
How can I fix this?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get reference to button field in the gridview.
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[1].Controls[1];
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
e.Row.Style["cursor"] = "hand";
e.Row.Attributes["onclick"] = _jsSingle;
}
}
}
You should be using something like e.Row.FindControl("linkbuttonid"). This will get you the required link button from the current row, then you can attach your handlers to the same and perform your logic

Accessing Gridview columns by Row.Cells

Hai I am using the following code to access columns in a row but always displays empty string when using Row.Cells[1].Text I am using this code in GridView Unload event handler.
Thanks inn advance.
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
I would suggest using the Gridview Databound event instead. Because the
Unload event occurs when the server control is unloaded from memory.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
}
Databound events occur after the server control binds to a data source.
To understand how gridview events work, look at MSDN
It could be done in Gridviews RowDataBound Event ie
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
String coltext = e.Row.Cells[1].Text
}
else if(e.Row.RowType == DataControlRowType.DataRow)
{
String coltext = e.Row.Cells[1].Text
}
}
Hope this helps.

Categories

Resources