asp:gridview textbox old value is post back - c#

<asp:GridView Width="300px" RowStyle-Height="20px" HeaderStyle-Height="25px" DataKeyNames="Id" ID="bankAccounts_List" CssClass="table" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Sorting Handle" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<span class="handle"><img src="../Images/sortingHandler.jpg" height="10" /></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account Number">
<ItemTemplate>
<asp:TextBox ID="grd_txtbankAccountNumber" Width="150" onblur="return OnBlurGridTextbox(this);" BorderStyle="None" BackColor="White" style="text-align:left;" runat="server" Text='<%# Eval("AccountNo") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Priority" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:TextBox ID="grd_txtPriority" Width="100" BorderStyle="None" BackColor="White" runat="server" style="text-align:center"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:LinkButton runat="server" ID="grd_linkbutton" OnClientClick="return EditGridButton(this)">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id" >
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
<ItemTemplate>
<asp:Label ID="grd_lblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSaveBankInformation" runat="server" Text="<%$Resources:Strings,save%>"
OnClick="btnSaveBankInformation_Click" />
protected void btnSaveBankInformation_Click(object sender, EventArgs e)
{
TextBox txtBankAccountNumber = bankAccounts_List.Rows[i].Cells[GetColumnIndexByName(bankAccounts_List.Rows[i], "Account Number")].FindControl("grd_txtbankAccountNumber") as TextBox;
}
What ever I change in any of the textbox, On save button I always gets the old binded textbox value not the New value I just entered.
Kindly Help me. and yes i am not binding GridView on PostBack again using !IsPostBack

if (!IsPostBack) //Bind grid only when IsPostBack is false
{
grd.DataSource = //data source ;
grd.DataBind(); // bind data
}

Related

Multiple DropDown list in GridView in C# and Asp.net like city,state,country

I want to develop 3 dropdownlists city, state, country. When I select the country then it should only show states which are in selected country same for city & state
ASP.NET CODE:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="manager" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" />
<asp:TemplateField HeaderText="ma_city" SortExpression="ma_city">
<EditItemTemplate>
<asp:DropDownList ID="ddl_city" runat="server" AutoPostBack="True" SelectedIndex='<%# Eval("city_id") %>' SelectedValue='<%# Bind("ma_city") %>' DataSourceID="city">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ma_city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ma_state" SortExpression="ma_state">
<EditItemTemplate>
<asp:DropDownList ID="ddl_state" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList5_SelectedIndexChanged1" SelectedIndex='<%# Bind("state_id") %>' SelectedValue='<%# Bind("ma_state") %>' DataSourceID="state">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ma_state") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ma_country" SortExpression="ma_country">
<EditItemTemplate>
<asp:DropDownList ID="ddl_country" runat="server" AutoPostBack="True" DataSourceID="country" DataTextField="name" DataValueField="Id" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged" SelectedIndex='<%# Eval("countrt_id") %>' SelectedValue='<%# Bind("ma_country") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ma_country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ma_pincode" HeaderText="ma_pincode" SortExpression="ma_pincode" />
<asp:BoundField DataField="Ma_phone_no" HeaderText="Ma_phone_no" SortExpression="Ma_phone_no" />
</Columns>
</asp:GridView>
and i also use below code in every event like (This is auto generate in visualstudio 2017 ):
DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
GridView1_SelectedIndexChanged(object sender, EventArgs e)
I use two type of code to get gridview reference by:
GridView gridView = (GridView)((((Control)sender).Page.Items[GridView1]));
GridViewRow gridViewRow = gridView.SelectedRow;
DropDownList dpl5 = (DropDownList)gridViewRow.FindControl("ddl_state");
DropDownList dpl6 = (DropDownList)gridViewRow.FindControl("ddl_city");
code:-
GridViewRow gridViewRow = GridView1.SelectedRow;
DropDownList dpl5 = (DropDownList)gridViewRow.FindControl("ddl_state");
DropDownList dpl6 = (DropDownList)gridViewRow.FindControl("ddl_city");
This both give me null as return.

How to get the selected value of a dropdownlist in a Gridview C#

I'm having a GridView to display few columns. when I click the edit button in the GridView, it displays a DropDownList for one column which has a list of values to select.
I need to select a value and click on the Update button to update the database with the latest selected value.
<asp:GridView ID="GrdAttributesView" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CssClass="well"
EmptyDataText="No Records to display."
OnPageIndexChanging="GrdAttributesView_PageIndexChanging"
OnRowCancelingEdit="GrdAttributesView_RowCancelingEdit"
OnRowEditing="GrdAttributesView_RowEditing"
OnRowUpdated="GrdAttributesView_RowUpdated"
OnRowUpdating="GrdAttributesView_RowUpdating"
OnSelectedIndexChanged="GrdAttributesView_SelectedIndexChanged"
OnSorting="GrdAttributesView_Sorting"
OnRowDeleting="GrdAttributesView_RowDeleting"
OnRowDeleted="GrdAttributesView_RowDeleted"
OnRowDataBound="GrdAttributesView_RowDataBound" PagerSettings-Mode="Numeric"
PagerStyle-Font-Bold="true" PageSize="20" ShowHeaderWhenEmpty="True">
<Columns>
<%-- <asp:TemplateField HeaderText="Globalized Indicator">
<ItemTemplate>
<asp:CheckBox ID="Chk_GlobalizedIndicatorGrid" runat="server" Width="55px" OnCheckedChanged="Chk_GlobalizedIndicatorGrid_CheckedChanged" Text='<%# Eval("GlobalizedInd") %>' Checked ='<%# Eval("GlobalizedInd") == DBNull.Value ? false : Convert.ToBoolean(Eval("GlobalizedInd")) %>' Enabled="false"/>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:CommandField HeaderText="Edit Record" ItemStyle-Width="150px" ShowEditButton="True" ShowHeader="True" ShowDeleteButton="true">
<ItemStyle Width="100px" />
</asp:CommandField>
<asp:BoundField HeaderText="Added Date" DataField="AddedDate" ItemStyle-Width="700px" ReadOnly="true" ControlStyle-Width="700px">
<ControlStyle Width="200px"></ControlStyle>
<ItemStyle Width="200px"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Created By" DataField="CreatedBy" ItemStyle-Width="700px" ReadOnly="true" ControlStyle-Width="700px">
<ControlStyle Width="100px"></ControlStyle>
<ItemStyle Width="100px"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Ticket Number" DataField="TicketNumber" ItemStyle-Width="200px" ReadOnly="true" ControlStyle-Width="700px">
<ControlStyle Width="200px"></ControlStyle>
<ItemStyle Width="200px"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Ticket URL" DataField="TIcketURL" ItemStyle-Width="200px" ReadOnly="true" ControlStyle-Width="700px">
<ControlStyle Width="600px"></ControlStyle>
<ItemStyle Width="600px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Hours Spent" ItemStyle-Width="100px" ControlStyle-Width="700px">
<ControlStyle Width="100px"></ControlStyle>
<ItemStyle Width="100px"></ItemStyle>
<ItemTemplate>
<asp:Label ID="LblHoursSpent" runat="server" Text='<%# Eval("HoursSpent")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="LblHoursSpent" runat="server" Text='<%# Eval("HoursSpent")%>' Visible="false"></asp:Label>
<asp:DropDownList ID="CmbHoursSpent" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Minutes Spent" ItemStyle-Width="100px" ControlStyle-Width="700px">
<ControlStyle Width="120px"></ControlStyle>
<ItemStyle Width="120px"></ItemStyle>
<ItemTemplate>
<asp:Label ID="LblMinutesSpent" runat="server" Text='<%# Eval("MinutesSpent")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="LblMinutesSpent" runat="server" Text='<%# Eval("MinutesSpent")%>' Visible="false"></asp:Label>
<%--<asp:DropDownList ID="CmbHoursSpent" AppendDataBoundItems="true" DataTextField="NumberofHoursSpent" DataValueField="HoursSpent" AutoPostBack="false" runat="server" OnSelectedIndexChanged="CmbHoursSpent_SelectedIndexChanged" enabled="true">
</asp:DropDownList>--%>
<asp:DropDownList ID="CmbMinutesSpent" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle Font-Bold="True" />
</asp:GridView>
The code behind is as follows:
protected void GrdAttributesView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && GrdAttributesView.EditIndex == e.Row.RowIndex)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlHours = (DropDownList)e.Row.FindControl("CmbHoursSpent");
ddlHours.DataSource = GenerateHoursList();
ddlHours.DataTextField = "NumberofHoursSpent";
ddlHours.DataValueField = "HoursSpent";
ddlHours.DataBind();
ddlHours.Items.FindByValue((e.Row.FindControl("LblHoursSpent") as Label).Text).Selected = true;
DropDownList ddlMinutes = (DropDownList)e.Row.FindControl("CmbMinutesSpent");
ddlMinutes.DataSource = GenerateMinutesList();
ddlMinutes.DataTextField = "NumberofMinutesSpent";
ddlMinutes.DataValueField = "MinutesSpent";
ddlMinutes.DataBind();
ddlMinutes.Items.FindByValue((e.Row.FindControl("LblMinutesSpent") as Label).Text).Selected = true;
}
}}
protected void GrdAttributesView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList ddlprice = (DropDownList)GrdAttributesView.Rows[e.RowIndex].FindControl("CmbHoursSpent");
String value = ddlprice.SelectedValue;
string MinutesSpent = (GrdAttributesView.Rows[e.RowIndex].FindControl("CmbMinutesSpent") as DropDownList).SelectedItem.Value;
}
Ideally in the RowUpdating method I should get the new value from the DropDown. but I don't understand why it gives the old value even though I select a different value.
I did a lot of reading and I saw different people using the same approach to get it done. But somehow it is not working for me.
Can someone help please?

How to add Controlls on runtime to a Gridview?

First of all the ASPcode, the problemdescription below.
<asp:GridView ID="GridViewContacts" runat="server" ForeColor="#333333" DataKeyNames="L_ID_CONTACT" AllowPaging="True" AllowSorting="True"
OnPageIndexChanging="GridViewContacts_PageIndexChanging" PageSize="25" AutoGenerateColumns="False" OnRowCommand="GV_Contacts_RowCommand" >
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandArgument="Edit" CommandName="Edit" Text="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonView" runat="server" CommandArgument="View" CommandName="View" Text="View">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="L_Name" runat="server" Text='<%# Eval("L_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Companydetails">
<ItemTemplate>
<asp:Label ID="L_Companydetails" runat="server" Text='<%# Eval("L_Companydetails") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMail">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False" HeaderText="ID_CONTACT" >
<ItemTemplate>
<asp:Label Visible="false" ID="L_ID_CONTACT" runat="server" Text='<%# Eval("L_ID_CONTACT") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<%-- //Stylesettings here--%>
</asp:GridView>
Okay, then in the CodeBehind I have a Select from my database, where i select the ID_Contact, the Name, the Companydetails, which can be 1 per Row only.
On the RowCreated event I get the UserID of the actual User, and I select all E-Mails the user has, can be 0-10 per row.
Now my problem is:
How can i insert Linkbuttons with the onClick-event in the description into this part of my code?
Like this:
<asp:TemplateField HeaderText="EMail">
<ItemTemplate>
<asp:LinkButton[i] runat="server" onClick="SendEmail">
</asp:Linkbutton[i]>
<asp:LinkButton[i] runat="server" onClick="SendEmail">
</asp:Linkbutton[i]>
</ItemTemplate>
</asp:TemplateField>
So i want to add those controlls with code into THIS TemplateField.
Is this possible ?
Thoughts i allready had:
This.GridViewContacs.Controlls.AddAt(index,Linkbutton)
But also no clue here how it should work.
Thanks in advance,
me
Easiest is to add a placeholder control to the ItemTemplate, as ItemTemplate has no ID.
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="emails" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
and then in RowDataBound event
if (e.Row.RowType == DataControlRowType.DataRow)
{
PlaceHolder emails = e.Row.FindControl("emails") as PlaceHolder;
if (emails != null)
{
LinkButton lbEmail = new LinkButton();
lbEmail.Text = "your text";
lbEmail.Click += new EventHandler(SendEmail);
emails.Controls.Add(lbEmail);
}
}
Of course, the example is simplified. You can easily extend it to your needs.

How to bind hovermenuextender control to a row with multiple columns in a dynamic gridview?

I wish to attach a hovermenu pop up to rows of a gridview which is dynamically populated from a datasource. I tried to set TargetcontrolId property of hovermenuextender control to row unique Id on occurence of rowdatabound event. But rather than appearing on the right side of each row, pop up is appearing on the right side of header row. Can somebody help me resolve this problem? I used below code for the same
CodeBehind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HoverMenuExtender hoverMenu =(HoverMenuExtender)e.Row.FindControl("hme2");
if (hoverMenu != null)
{
hoverMenu.TargetControlID = hoverMenu.Parent.Parent.UniqueID;
}
}
}
}
Aspx page:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" ShowFooter="false" ShowHeader="false"
OnRowEditing="GridView1_RowEditing" OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating"
GridLines="None" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText ="Talk Title">
<ItemTemplate>
<asp:Label Font-Bold="true" ID="lbltalktitle" runat="server" Text='<%#Eval("Talk_Title") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Brand">
<ItemTemplate>
<asp:Label ID="lblBrand" runat="server" Text='<%# Eval("Brand") %>' /></td>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Franchise">
<ItemTemplate>
<asp:Label ID="lblFranchise" runat="server" Text='<%# Eval("Franchise") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="programmetype">
<ItemTemplate>
<asp:Label ID="lblprg" runat="server" Text='<%# Eval("programmetype") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="programmetype">
<ItemTemplate>
<asp:Label ID="lblsaledforce" runat="server" Text='<%# Eval("salesforce") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Start_Date">
<ItemTemplate>
<asp:Label ID="lblstartdate" runat="server" Text='<%# Eval("Start_Date") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="End_Date">
<ItemTemplate>
<asp:Label ID="lblenddate" runat="server" Text='<%# Eval("End_Date") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<cc1:HoverMenuExtender ID="hme2" runat="server" TargetControlID="lblenddate"
PopupControlID="PopupMenuX" HoverCssClass="popupHover" PopupPosition="Right"/>
<asp:Panel CssClass="popupMenu" ID="PopupMenuX" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" Text="Edit" /><br />
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Text="Delete" />
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle ForeColor="Black" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
AjaxControlToolkit.HoverMenuExtender ajxhovermenu = (AjaxControlToolkit.HoverMenuExtender)e.Row.FindControl("ahm_1");
e.Row.ID = e.Row.RowIndex.ToString();
ajxhovermenu.TargetControlID = e.Row.ID;
}
}
Give TargetControlID of the HoverMenuExtender in RowCreated event of GridView and set it as the RowID

Nested grid edit doesn't work

I'm stuck on this problem where I have grid within grid where the nested grid is editable. I can't get the edit working. What's complicates things is that the nested grid is inside a modal popup extender.
It works to the point where I click on the edit button. Then the EmptyDataText property kicks in with the message. If I click on it second time the grid opens in edit mode but the update/cancel buttons don't work. The cancel button when click displays the EmptyDataText property.
Another issue is that this nested grid doesn't use a object data source so I'm going to have to all the updating and deleting in the code file. Not sure now to do that either.
I would like some advice as to how to make this corrected. Here is the code:
<asp:GridView ID="gvForum" runat="server" DataSourceID="odsForumApproval" DataKeyNames="id" Width="200px"
RepeatColumns="1" DataKeyField="id" CssClass="gridview"
AutoGenerateColumns="False" GridLines="None" OnSelectedIndexChanged="_OnCommand">
<AlternatingRowStyle CssClass="altbgcolor" />
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:Label runat="server" ID="lblTitle" Text='<%# Bind("Title") %>' />
<asp:Panel id="div" runat="server" align="center" class="confirm" style="display:none" >
<asp:GridView runat="server" ID="gvForumDetail" AutoGenerateColumns="False" DataKeyNames="id"
AllowPaging='true' CssClass="gridview"
AllowSorting="true" PageSize="5" CellPadding="5" OnRowEditing="gvForumDetail_OnRowEditing"
OnRowCancelingEdit="gvForumDetail_CancelRecord" >
<AlternatingRowStyle CssClass="altbgcolor" />
<RowStyle VerticalAlign="Top" HorizontalAlign="Left" />
<HeaderStyle CssClass="greenbar" ForeColor="White" /> <Columns>
<asp:BoundField DataField="id" ReadOnly="true" Visible="false" />
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label runat="server" ID="lblTraining" Text='<%# Bind("title") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtTraining" Text='<%# Bind("title") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Post Message">
<ItemTemplate>
<asp:Label runat="server" ID="lblCompletionDate" Width="250" Text='<%# Bind("description") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtDescription" Text='<%# Bind("description") %>' TextMode="MultiLine" Rows="5" Width="250" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Posted By">
<ItemTemplate>
<asp:Label runat="server" ID="lblRecurence" Text='<%# Bind("MemberName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Posted Date">
<ItemTemplate>
<asp:Label runat="server" ID="lblNotes" Text='<%# Eval("itemdate", "{0:d}") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox runat="server" ID="cbxApproved" Text='<%# Bind("approved") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowCancelButton="true" ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<br />
<Club:RolloverLink ID="btnClose" runat="server" Text="Close" />
</asp:Panel>
<ajaxToolKit:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
TargetControlID="lblTitle"
PopupControlID="div"
CancelControlID="btnClose"
BackgroundCssClass="modalBackground" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
</Columns>
Code behind:
public void _OnCommand(object sender, EventArgs e)
{
ObjectDataSource ods = new ObjectDataSource();
ods.ID = "ods_ForumDetail";
ods.EnableViewState = true;
ods.TypeName = "ForumApproval";
ods.SelectMethod = "GetForumById";
string id = "";
int rowIndex = gvForum.SelectedIndex;
id = gvForum.DataKeys[rowIndex].Value.ToString();
ods.SelectParameters.Add("id", System.TypeCode.Int32, id);
var ModalPopupExtender1 = (ModalPopupExtender)(gvForum.SelectedRow.FindControl("ModalPopupExtender1"));
ModalPopupExtender1.Show();
var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail"));
gvForumDetail.DataSource = ods;
gvForumDetail.DataBind();
}
protected void gvForumDetail_OnRowEditing(Object sender, GridViewEditEventArgs e)
{
var ModalPopupExtender1 = (ModalPopupExtender)(gvForum.SelectedRow.FindControl("ModalPopupExtender1"));
ModalPopupExtender1.Show();
var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail"));
gvForumDetail.EditIndex = e.NewEditIndex;
gvForumDetail.DataBind();
}
protected void gvForumDetail_CancelRecord(object sender, GridViewCancelEditEventArgs e)
{
var ModalPopupExtender1 = (ModalPopupExtender)(gvForum.SelectedRow.FindControl("ModalPopupExtender1"));
ModalPopupExtender1.Show();
var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail"));
gvForumDetail.EditIndex = -1;
gvForumDetail.DataBind();
}
I will try to answer part of your question, updating and deleting the code. you need to take advantage of the "onrowcommand" for the inner gridview (OnRowCommand).

Categories