Get multiple textbox values from repeateritem control that triggered event - asp.net

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");
....
}
}

Related

How to get values from Controls which are in GridView?

I am new to asp.net website developer.
In my website I use GridView Control.In the GridView row i place some controls like
TextBox,DropDownList.
I can display the values in GridView.
But my requirement is ,Get the values from TextBox and DropdownList Which are existed in GridView.
Please help me to go forward..
thank you,
bye..
You need to access them by row. This code project article explains it in detail.
TextBox tb = (TextBox)gridview1.Rows[0].FindControl("idOfTextBox");
It above statement will find control in the first row of grid which has id idOfTextBox and type is textbox.
You can directly get any value of any control by casting directly, without using an extra textbox or dropdown variable.
Example:
string val = ((TextBox)gridview1.Rows[0].FindControl("TextBox_ID")).Text;
Hope it helps :)
Subscribe the RowDataBound event(this will be fired on each row available in gridview) of gridview and access like stated below,
protected void grdBillingdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//these controls are available inside gridview
HiddenField hdnNagetiveAmt = (HiddenField)e.Row.FindControl("hdnNagetiveAmt");
DropDownList ddlFeeType = (DropDownList)e.Row.FindControl("ddlFeeType");
TextBox txtFeeDesc = (TextBox)e.Row.FindControl("txtFeeDesc");
Button btnUpdate = (Button)e.Row.FindControl("btnUpdate");
}
}
not only text box we can get values from all the controls that used inside gridview that placed by using itemTemplate or simply a Bound field .
you need to loop for each row to get the values
foreach (GridViewRow row in grd_popup_details.Rows)
{
//get control values
}
Refference link

gridview button field

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.

dynamic columns disappears after postback

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

Get Value from Dynamic User Control On PostBack Asp.Net

I created an item user_control that has a textbox, button, etc. which will intentionally collect the total quantity of items the user wants.
I dynamically create a few intances of user_control on page_load. If you click the add button for the item quantity it will add to a session variable. however when the user enters a different quantity in the textbox and clicks the add button the total is of the original value of the textbox.
How to get the value the the user has typed in to the textbox to add to total???
You need to assign the same ID to the dynamic control after each postback, when you recreate the control. If you don't assign it the same ID, ViewState cannot populate the value.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
TextBox txt = new TextBox();
txt.ID = "txt1";
PlaceHolder1.Controls.Add(txt);
}

ASP.NET 2 Gridviews share the same OnSelectedIndexChanged event

I have 2 Gridviews which share the same OnSelectedIndexChanged event.
How do I get the incoming Gridview the one which fired so that I can pass that GridView to DetailsView. In that DetailsView I need to access the selected Gridview columns.
Thanks In Advance.
first parameter is "sender" as an object and you can cast it to a gridview object than check its ID.
GridView grd = (GridView) sender;
The "sender" parameter of the OnSelectedIndexChanged event should be the GridView that the event came from. You can get at it like this:
public void MyGrid_OnSelectedIndexChanged(object sender, EventArgs e)
{
GridView grid = sender as GridView;
if (grid != null)
{
// Do something with grid
}
}

Resources