I have a gridview with a buttonfield. I want to update a table in my database and change the button image on button click. What is best event for that where i can access the row index as well?
I have tried using the RowCommand event but cant access the row index from that event
You can try with this code - based on CommandArgument
void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Test")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = CustomersGridView.Rows[index];
}
}
Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.80).aspx
Instead of asp:ButtonField use asp:TemplateField and an asp:Button inside that. Set a CommandName say, MyCommand. Now inside the RowCommand event, do this
var clickedRow = (GridViewRow)((Button)sender).NamingContainer;
var clickedIndex = clickedRow.RowIndex;
I've just checked the docs here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
If you look at the "Note" section (below for convenience) you should be able to get the RowIndex from the CommandArguement property:
Note To determine the index of the row that raised the event, use the
CommandArgument property of the event argument that is passed to the
event. The ButtonField class automatically populates the
CommandArgument property with the appropriate index value. For other
command buttons, you must manually set the CommandArgument property of
the command button. For example, you can set the CommandArgument to
<%# Container.DataItemIndex %> when the GridView control has no paging
enabled.
Related
I have a Repeater control that outputs various forum posts on the page.
Within each repeater row, I have a LinkButton and 4 TextBoxes that contain values.
When I click on one of the LinkButtons, in my event handler code, I want to get the values that are in each of the 4 TextBoxes that correspond with that one particular repeater item/row.
I can repeat through each item within the repeater, but I am only interested in the values that exist in the 4 textboxes that sat alongside the LinkButton that was clicked/that triggered the event. I'm not interested in any of the Textbox values that belong to the other rows/items within the repeater.
What's the best way to do this?
You can use with ItemCommand event and e.Item.FindControl
Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.repeater.itemcommand.aspx
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName == "YourCommand" ) //Adjust your CommandName
{
var textbox1 = (TextBox)e.Item.FindControl("YourIdTextBox1"); //Adjust your Id of TextBox in row
var textbox2 = (TextBox)e.Item.FindControl("YourIdTextBox2");
var textbox3 = (TextBox)e.Item.FindControl("YourIdTextBox3");
var textbox4 = (TextBox)e.Item.FindControl("YourIdTextBox4");
....
}
}
I have a GridView with some BoundFields and two TemplateFields. In these two TemplateFields, I dynamically create UserControls containing a DropDownList and a TextBox, which users can modify.
When I try to get the values of the controls after a PostBack, the values in BoundFields are still there but my dynamic controls disappears. I can create them again but it won't get the user's values... How can I get these values before they're lost?
Here's some of my code:
In the RowDataBound event:
Select Case type
Case "BooleanBis"
e.Row.Cells(2).Controls.Clear()
Dim list1 As BooleanBisList = New BooleanBisList(avant, False)
e.Row.Cells(2).Controls.Add(list1)
e.Row.Cells(4).Controls.Clear()
Dim list2 As BooleanBisList = New BooleanBisList(apres, True)
e.Row.Cells(4).Controls.Add(list2)
Case "Boolean"
e.Row.Cells(2).Controls.Clear()
Dim list3 As BooleanList = New BooleanList(avant, False)
e.Row.Cells(2).Controls.Add(list3)
e.Row.Cells(4).Controls.Clear()
Dim list4 As BooleanList = New BooleanList(apres, True)
e.Row.Cells(4).Controls.Add(list4)
End Select
In my button click event, I try to get the user control :
Case "String"
temp.ChampValeurApres = DirectCast(Tableau1.Rows(i).Cells(selectedColumn).Controls(1), TextBox).Text
but i get the error that it doesn't exist.
You should create dynamic controls in RowCreated instead of RowDataBound because this event gets fired on every postback whereas RowDataBound only will fire when the GridView gets databound to it's DataSource.
Dynamically created controls must be recreated on every postback with the same ID as before, then they retain their values in the ViewState and events will fire correctly(f.e. a DropDownList's SelectedIndexChanged event).
So you should create them in RowCreated and "fill" them in RowDataBound(f.e. the DropDownList datasource/Items or a TextBox-Text).
I had been using:
EnableViewState="false"
in the GridView attributes. Removing it solved my problem!
I just did
protected void Page_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
// Put the selected items which u want to keep on postback
}
else
{
//regenerate auto created controls
}
}
and it worked as well
I am creating a simple ASP page that has a Repeater control. This repeater control contains LinkButtons; so that as the repeater increases in item size, the amount of LinkButtons also increases. My question is, how can I identify uniquely the RepeaterItem to which I click the LinkButton?.
I though of using a Foreach to search through the RepeaterItems but I don't know what conditions should I use to identify the Linkbutton I cliked. By the way, each LinkButton have the same name and same ID.
My goal is to get the RepeaterItem in which that Linkbutton belongs
Thanks,
Y_Y
Assign Command/ComandArgument for your LinkButton
Assign handler 'OnCommand' for your Repeater and create event handler
In the handler you get RepeaterCommandEventArgs - use item property to access
You can use:
- Tag property of LinkButton to store object to identify the LinkButton
- Included hidden field in Repeater template to store something to identify Linkbutton.
- Using CommandArgument of LinkButton
- ...
You can look at using what is called "ItemCommand"
or you can set the CommandArgument of your LinkButton.
protected void lnkButton_Click(object sender, EventArgs e) {
LinkButton _sender = (LinkButton)sender;
string argument = _sender.CommandArgument;
}
The common solution for this scenario is to use the CommandName and/or CommandArgument properties of the button. Just bind the CommandArgument to the ID of the objects you put into the repeater. The command argument can be accessed in the click handler.
The row has a LinkButton that when clicked needs to highlight the row.
Code so far:
protected void linkbutton1_Click(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;
GridViewRow g = (GridViewRow)l.Parent; // what is the correct way to do this?
//g.Style etc etc
}
first of all set the "CommandName" property of LinkButton to "select",
then in the selectedIndexChanging event of gridview write below code:
for (int i = 0; i < GridView1.Rows.Count;i++ )
GridView1.Rows[i].BackColor = System.Drawing.Color.White;
GridView1.Rows[e.NewSelectedIndex].BackColor = System.Drawing.Color.Cornsilk;
Make use of the RowCommand event of the GridView instead of the Click event of the LinkButton.
Then you can have a CommandName on the LinkButton such as "HighlightRow" and do something like the following:
Select Case e.CommandName
Case "HighlightRow"
e.item.row.attributes("class") = "highlight"
End Select
Sorry its in VB.NET and not C#
1.) Set Command Name Property to "Select"
2.) Change style either on code behind as shown by #Raymond or give set Cssclass attribute for
SelectedRowStyle of gridview to CssClass="selecterowstyle"
.selectedRowstyle
{
background-color:#EAEAEA;
}
I have added a RadControl, RadGrid and I need to add a radComboBox in the edit mode.
When user clicks on the radComboBox, I need to get both "text" and "value" of the radComboBox to save to table when user updates values.
<telerik:RadComboBox ID="RadComboBox1" Runat="server"
DataSourceID="SqlDataSource1" DataTextField="docCategoryName"
DataValueField="docCategoryID" Height="200px" Skin="Vista">
When user selects from the radComboBox, I need to get the value of DataTextField & DataValueField into a HiddenField.
The approach described by Program.X is very good. This, of course, works if you're using a RadCombobox inside of a GridTemplateColumn in RadGrid.
RadGrid also provides a built-in GridDropDownColumn that can automatically render a RadCombobox during edits. If you choose to use the built-in column type, you need to programmatically set the client-side event handlers for the rendered RadGrid, like this:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
//Find GridEditableItems when in Edit mode
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
//Get reference to item (i.e. Row)
var item = e.Item as GridEditableItem;
//Get reference to auto-generated RadCombobox in
//specific column (in this case, a column called Title)
var rcb = item["Title"].Controls[0] as RadComboBox;
if (rcb == null)
return;
//Customize the RadCombobox properities
rcb.OnClientSelectedIndexChanged = "onselectedindexchanged";
}
}
Where "onselectedindexchanged" is the name of a client-side JavaScript function on your page designed to handle the RadCombobox event.
Either of these approaches should enable you to achieve your goal.
Telerik are the best people to help you with this, but from their site (and from memory):
http://www.telerik.com/help/aspnet-ajax/combo_clientsideonclientselectedindexchanged.html
If you have your radCombo:
<telerik:RadComboBox
ID="RadComboBox1"
runat="server"
OnClientSelectedIndexChanged="OnClientSelectedIndexChanged">
</telerik:RadComboBox>
With your JS event:
<script language="javascript" type="text/javascript">
function OnClientSelectedIndexChanged(sender, eventArgs)
{
var item = eventArgs.get_item();
// get the text and value elements
var text=item.get_text();
var val=item.get_value();
$('#hiddenField').val(val);
}
</script>
Where hiddenField is the ID of the hidden field.