I have a grid view with a SelectedIndexChanged event that fires when the user selects a row.
Now in the grid view row, I add a drop down list using TemplateField, and in the grid view RowDataBound event I add code for firing the SelectedIndexChanged event of the drop down list.
So, when user click on drop down list the first event that fires is the SelectedIndexChanged of the grid view, then page goes into post back and the selection of drop down list is lost.
The SelectedIndexChange of drop down list fires only if user is faster on changing selection than page goes into post back.
I need that when user select a row with drop down list grid view's event wait for the drop down list selection and after first event that can fire is drop down list changing and at last grid view's SelectedIndexChanged event.
Is this possible?
some code :
<asp:GridView ID="grEventi" runat="server" BackColor="White"
ShowHeaderWhenEmpty="True" AutoGenerateColumns="False"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" Width="100%" OnRowDataBound="grEventi_RowDataBound"
onselectedindexchanged="grEventi_SelectedIndexChanged" >
<SelectedRowStyle CssClass="selectedRow" />
<Columns>
<asp:BoundField DataField="Elenco Eventi" HeaderText="Evento" />
<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-Width="150" HeaderText="Data Inizio">
<ItemTemplate>
<asp:Label ID="lbl_data" runat="server" Text="" Visible="false" >
</asp:Label>
<asp:DropDownList ID="ddl_data" runat="server" Visible="false" OnSelectedIndexChanged="ddl_dat_SelectedIndexChanged" ClientIDMode = "Static" class ="calendar">
</asp:DropDownList>
</asp:TemplateField>
</Columns>
</asp:GridView>
code-behind:
protected void ddl_dat_SelectedIndexChanged(object sender, EventArgs e)
{
//your logic goes here
string test = "";
}
protected void grEventi_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", this.ClientScript.GetPostBackEventReference((Control)sender, "Select$" + e.Row.RowIndex));
DropDownList ddl_dat = (DropDownList)e.Row.FindControl("ddl_data");
ddl_dat.SelectedValue = DataBinder.Eval(e.Row.DataItem, "data inizio").ToString();
ddl_dat.Visible = true;
ddl_dat.DataTextFormatString = "{0: ddd d/MM/yyyy HH:mm}";
ddl_dat.DataTextField = "data inizio";
ddl_dat.DataValueField = "data inizio";
ddl_dat.DataSource = mydata;
// ddl_dat.AutoPostBack = true;
ddl_dat.DataBind();
}
else
{
Label lbl_data = (Label)e.Row.FindControl("lbl_data");
lbl_data.Visible = true;
DateTime date=(DateTime)dr.Row["data inizio"];
lbl_data.Text = date.ToString("ddd d/MM/yyyy HH:mm");
}
}
protected void grEventi_SelectedIndexChanged(object sender, EventArgs e)
{
//my logic code
}
Related
I have a gridview in which I have manually generated a column for checkboxes as a HeaderTemplate as below
<asp:GridView ID="gvDB" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowDataBound="gvDB_RowDataBound" <asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkSelectHeader" AutoPostBack="true" OnCheckedChanged="chkSelectHeader_CheckedChanged" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" AutoPostBack="true" runat="server" OnCheckedChanged="chkSelect_CheckedChanged1" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And OnRowDataBound I'm dynamically generating controls and adding it to the each row
e.Row.Cells[rowIndex].Controls.Add(control);
And they are binding to the columns as expected.But my chkSelectHeader_CheckedChanged chkSelect_CheckedChanged1 events are not firing.
Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddTemplatesToGrid();
}
BindDataToGridView();
}
public void AddTemplatesToGrid()
{
DataTable dt = new DataTable();
foreach (Employees emp in EmployeesList)
{
TemplateField tfield = new TemplateField();
tfield.HeaderText = emp.Name;
gvDataEntry.Columns.Add(tfield);
}
}
You call BindDataToGridView on every postback which will discard events.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddTemplatesToGrid();
}
BindDataToGridView();
}
Include BindDataToGridView() in the !Page.IsPostBack-check.
I have a grid which contains a edit button . When i click the edit button and debug it does not hit to the selected index change event . There are no build errors
code behind the grid
public void btnModemDetailsEdit_Click(object sender, EventArgs e)
{
isEdit = true;
}
protected void gridModemDetails_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt32(GridModemDetails.DataKeys[GridModemDetails.SelectedIndex].Values["gridModemDetails_SelectedIndexChanged"].ToString());
}
<asp:GridView ID="GridModemDetails" runat="server" Width="435px"
DataKeyNames="ModemId" AllowPaging="True"
OnSelectedIndexChanged="gridModemDetails_SelectedIndexChanged"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Edit" Visible="True" >
<ItemTemplate>
<asp:LinkButton ID="btnModemDetailsEdit"
AccessibleHeaderText="Edit"
ButtonType="Button"
Text="Edit"
HeaderText="Edit"
runat="server"
OnClick="btnModemDetailsEdit_Click"/>
</ItemTemplate>
</asp:TemplateField>
The GridView's SelectedIndexChanged event is tied to the RowCommand event.
The simple way to get the SelectedIndexChanged event to fire is to use the AutoGenerateSelectButton property of the GridView, like this:
<asp:GridView AutoGenerateSelectButton="true"
This will add a button to each row with the text Select and when clicked, the SelectedIndexChange event will fire.
In the case of your edit button, you can use the CommandField in the grid view markup, like this:
<asp:GridView ...>
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
Now in your code-behind, you can handle the RowCommand event, like this:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
// Get the actual row
GridViewRow theGridViewRow = GridView1.Rows(e.RowIndex);
// Do something with grid view row here
}
}
I have a GridView with each row containing a checkbox, and the header of that column is a checkbox with checkAll functionality:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" ToolTip="Select this order" AutoPostBack="true" OnCheckedChanged="chkSelect_OnCheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
...More BoundFields
</Columns>
The javascript behind the SelectAllCheckboxes()
function SelectAllCheckboxes(spanChk) {
// Added as ASPX uses SPAN for checkbox
var oItem = spanChk.children;
var theBox = (spanChk.type == "checkbox") ?
spanChk : spanChk.children.item[0];
xState = theBox.checked;
elm = theBox.form.elements;
for (i = 0; i < elm.length; i++)
if (elm[i].type == "checkbox" &&
elm[i].id != theBox.id) {
//elm[i].click();
if (elm[i].checked != xState)
elm[i].click();
//elm[i].checked=xState;
}
}
My GridView basically contains sales (orders) from my website, so dollar amounts. The OnCheckedChanged event adds (if checked) or subtracts (if unchecked) the current row's price from a totalAmount that is displayed on the page.
This all works great except that when I click the checkAll checkbox, all of the OnCheckedChanged events for all of the row check boxes fire and it takes a long time to process it all. Since all that I am doing in the OnCheckedChanged method is summing amounts, is there a way that I can NOT call the events for the individual checkboxes and just make one call to a separate method that will grab all the GridView rows and sum them all at once?
If you consider doing it all on the server in one postback, you could try this:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" ToolTip="Select this order" AutoPostBack="true" OnCheckedChanged="chkAll_OnCheckedChanged"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" ToolTip="Select this order" AutoPostBack="true" OnCheckedChanged="chkSelect_OnCheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
On the code behind:
protected void chkSelect_OnCheckedChanged(object sender, EventArgs e)
{
//Process checked item
}
protected void chkAll_OnCheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow item in grdTest.Rows)
{
CheckBox ckb = (CheckBox)item.FindControl("chkSelect");
//This will not call the individual event
ckb.Checked = ((CheckBox)sender).Checked;
//Process checked item
}
}
How can I change the value of a textbox whenever a dropdownlist within a gridview has its value changed?
On page load, the textbox shows the selected value, but when I change the selection of the dropdownlist, the textbox value doesn't change.
The code is below.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:TemplateField HeaderText="Entry">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duty">
<ItemTemplate>
<asp:DropDownList ID="duty" runat="server" OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" autopostback="true" EnableViewState="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The code behind is below.
protected void ddl1_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
Duty dy = new Duty();
dt = dy.getdutyid(Convert.ToInt32(dropcontractid.SelectedValue));
DropDownList ddl = (DropDownList)sender;
ddl.DataSource = dt;
ddl.DataTextField = "dutyid";
ddl.DataValueField = "dutyid";
ddl.DataBind();
TextBox1.Text = ddl.SelectedValue;
}
}
You need to use SelectedIndexChanged handler to show selected value:
Markup:
<asp:DropDownList ID="duty" runat="server" OnLoad="ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged"></asp:DropDownList>
Code-behind:
protected void duty_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
DropDownList duty= (DropDownList) gvr.FindControl("duty");
TextBox1.Text = duty.SelectedItem.Value;
}
I had a similar problem using the DropDownLists in GridView. My solution was to adjust the onLoad for the dropdown so that it wouldn't re-write the DropDownList on every post back. This way if there's something there then it won't re-populate it.
protected void dropDownLoad(object sender, EventArgs e)
{
DropDownList dropDown = sender as DropDownList;
if (dropDown.SelectedValue == null || dropDown.SelectedValue == "")
{
// Your Code to populate table
}
}
You should look into using data binding instead. You can bind the textbox.Text to the selecteditem.value, this will ensure that proper updating takes place
this happens to me once then i code like this... but i didnt use the onLoad attribute, tell me if this works,
<asp:TemplateField HeaderText="duty" SortExpression="duty">
<EditItemTemplate>
<asp:TextBox ID="duty" runat="server" Text='<%# Bind("duty_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblduty" runat="server" Text='<%# Eval("duty_Name") %>' />
<asp:DropDownList ID="ddlduty" runat="server" CssClass="dropdownlist"
OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" Visible = "false"
>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="5%" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
I want to access an object (hyperlink or button) within a templatefield in a gridview.
How do I do this?
Assuming that you're binding data to it, you'll want to look at doing that inside the RowDataBound event.
Here's an example of how to retrieve a control within a Template Field:
.aspx:
<asp:GridView ID="GridView1" Runat="server"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Template Field">
<ItemTemplate>
<asp:Button ID="btnTest" Runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnTest = (Button)e.Row.FindControl("btnTest");
btnTest.Text = "I'm in a Template Field";
}
}
You can use it on RowDataBound or click event of your template control like
TextBox txtTemp= (TextBox )e.Row[e.RowIndex].FindControl("yourControlName");
string someText=txtTemp.Text;