how to find control in edit item template?
You need to databind the GridView
again to be able to access the control in the EditItemTemplate
. So try this:
int index = e.NewEditIndex;
DataBindGridView(); // this is a method which assigns the DataSource and calls GridView1.DataBind()
DropDownList DdlCountry = GridView1.Rows[index].FindControl("DdlCountry") as DropDownList;
But instead i would use RowDataBound
for this, otherwise you're duplicating code:
protected void gridView1_RowDataBound(object sender, GridViewEditEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList DdlCountry = (DropDownList)e.Row.FindControl("DdlCountry");
// bind DropDown manually
DdlCountry.DataSource = GetCountryDataSource();
DdlCountry.DataTextField = "country_name";
DdlCountry.DataValueField = "country_id";
DdlCountry.DataBind();
DataRowView dr = e.Row.DataItem as DataRowView;
Ddlcountry.SelectedValue = value; // you can use e.Row.DataItem to get the value
}
}
}
Can't reach a control from EditItemTemplate in a GridView
instead of commandfield, try using like
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="20%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="LnkManageTitle" runat="server" Text="Manage Title" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LnkManageTitle" runat="server" Text="Save" CommandName="Update"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
rest seems to be fine.
For Updating, Call OnRowUpdating="Gridview1_RowUpdating"
on html page and on CS page declare an event.
Protected Void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
Don't forget to call CommandName="Update"
on a LinkButton
How to change a value of a control in EditItemTemplate in GridView
In the RowEditing
event, you can set the EditIndex
value and re-bind the data to the GridView:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void GridView1_Cancel(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView();
}
Then, in the RowDataBound
event, you can access the controls of the row:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == GridView1.EditIndex)
{
TextBox txtBox = e.Row.FindControl("TweetTB") as TextBox;
...
}
else
{
...
}
}
Accessing controls in the edititemtemplate of a listview
I've figured out a way to do what I need to do, though I'm not terribly happy with it.
protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (UnitsLV.EditIndex > -1)
{
// Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
}
}
ASP.NET GridView EditTemplate and find control
I got the answer.
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (this.GridView1.EditIndex != -1)
{
Button b = GridView1.Rows[GridView1.EditIndex].FindControl("Button1") as Button;
if (b != null)
{
//do something
}
}
}
How can I get a control inside my EditItemTemplate?
You have these textboxes declared in your edit template. These will only show up when your mode has been set to edit. I'm guessing this hasn't happened yet when the ModeChanging event is fired.
Put your code in your ModeChanged event, and check to see that you're editing.
void DetailsView1_ModeChanged(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode != DetailsViewMode.Edit)
return;
foreach (DetailsViewRow row in DetailsView1.Rows)
{
var textbox = row.FindControl("txtName");
}
}
Gridview event to access controls in ItemTemplate & EditItemTemplate?
Just add a hidden field to EditTemplate that stores the value of WasSold
data item as in code below.
In your RowUpdating
event, you can find the hidden field and get its value, then compare it with drop down value.
Markup to include hidden field in EditTemplate
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField id="hdnWasSold" runat="server" Value='<%# Eval("WasSold").ToString() %>' />
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
C# code to get the hidden field value in RowUpdating event
HiddenField hdnWasSold = (HiddenField)GridView_Sales.Rows[e.RowIndex].FindControl("hdnWasSold");
string wasSoldValue = hdnWasSold.Value;
find Control in TemplateField of GridView with jquery or javascript
Thanks for including the GridView example. Now that I can see what you are attempting, I have a much better answer for you.
First, make a slight change to the button template, change out CommandArgument
for OnClientClick
and since you are using this button client side instead of posting back to the server, you can simplify it like this:
<asp:Button ID="btn" Text='btn' OnClientClick='<%# Eval("ID", "YourJavascriptFunction({0} - 1); return false;") %>' runat="server" CausesValidation="false" />
I have the click event call your JavaScript function and it sends in a parameter of the server side resolved id. Notice I subtract 1 first though. This is because the server side ASP.Net Eval function give the ID starting at 1. But, each of the ids that get generated for your text input elements start with a zero base.
Now look at the JavaScript function below.
// Clicking the first button sends in a 0, second sends in a 1, etc.
function YourJavascriptFunction(id) {
// each of the TextBox1 elements has an ASP.Net server side
// generated id that ends with GridView2_Textbox1_0,
// GridView2_Textbox1_1, etc.
let selectId = "input[id$='GridView2_Textbox1_" + id + "']";
// The text we use in the querySelector function states
// find the DOM element with a tag of "input" where the id
// ends with . . . The $ in id$= is the part that says the
// value must "end with"
let textBox1 = document.querySelector(selectId);
// Now that we have TextBox1 from the same row as the button,
// getting the value is easy.
alert(textBox1.value);
}
I left off a jQuery example as this querySelector command works in almost every browser including IE8 and above, so you shouldn't need jQuery for something this simple.
Let me know if I can help further.
how to access controls in ListView EditItemTemplate
You should try this.
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (dataItem.DisplayIndex == ListView1.EditIndex)
{
TextBox tb = e.Item.FindControl("tbFK_MenuID") as TextBox;
}
}
}
Related Topics
How to Cast a Generic Enum to Int
Topmost Form, Clicking "Through" Possible
Creating a Database Programmatically in SQL Server
Create an Application That Will Expire After a Trial Period
C# Datetimes: Conversion for Different Time Zones
Direct Method from SQL Command Text to Dataset
Displaying Tooltip on Mouse Hover of a Text
Get the Decimal Part from a Double
Fastest Way to Remove White Spaces in String
C#: How to Add Subitems in Listview
Xmlserialize a Custom Collection with an Attribute
Cannot Deserialize JSON Array into Type - JSON.Net
Modify Xml Existing Content in C#
How to Convert a String Length to a Pixel Unit