I am having trouble determining where the bug is for this problem.
I have an ASP.NET Master.Page web application that has a Gridview. I am using the jquery-chosen plugin for a dropdown list in the EmptyDataTemplate (or FooterControl if there is data).
On initialization if there is no data for the grid, the dropdown is populated and displays correctly. If the grid has items in it and I delete all of them, so that there is no data, the dropdown does not display any data. The DataBound event is called and the DataTable has all of the correct data in it. It is bound to the dropdown. But do items appear in the list.
This is my markup:
<div id="DelegateGridWrapper">
<asp:GridView ID="DelegateInfoGridView" runat="server"
AutoGenerateColumns="false" Caption="Delegate Information"
CaptionAlign="Top" CssClass="grid" RowStyle-Wrap="true"
HorizontalAlign="Left" ShowFooter="true"
AllowPaging="true" PageSize="5" ShowHeaderWhenEmpty="false" onrowediting="DelegateInfoGridView_RowEditing"
onrowcancelingedit="DelegateInfoGridView_RowCancelingEdit" onrowdeleting="DelegateInfoGridView_RowDeleting"
onrowupdating="DelegateInfoGridView_RowUpdating"
ondatabound="DelegateInfoGridView_DataBound"
onrowcommand="DelegateInfoGridView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Recipient ID">
<ItemTemplate>
<asp:Label ID="deligvLblRecipientID" runat="server" Text='<%# Bind("RecipientID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delegate" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:Label ID="deligvLblRecipientName" runat="server" Text='<%# Bind("RecipientName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="deligvDDLRecipientName" runat="server" ClientIDMode="Static"
data-placeholder="Choose delegate…" class="chosen-single">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<asp:Label ID="deligvLblActive" runat="server" Text='<%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="deligvDDLActive" runat="server" Text='<%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %>'>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="deligvDDLActiveInsert" runat="server">
<asp:ListItem Selected="True">Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="deligvEditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" CssClass="gridActionbutton">
</asp:Button>
<asp:Button ID="deligvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete" ClientIDMode="Static"
Text="Delete" CssClass="gridActionbutton" OnClientClick="return confirm('Are you sure you want to delete this Delegate Information?')" >
</asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="deligvUpdateButton" runat="server" CausesValidation="False" CommandName="Update"
Text="Update" CssClass="gridActionbutton"></asp:Button>
<asp:Button ID="deligvCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" CssClass="gridActionbutton"></asp:Button>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="deligvAddButton" runat="server" CommandName="Add" Text="Add Delegate" Width="90%" CausesValidation="false"
CssClass="gridActionbutton">
</asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<tr>
<th>Recipient ID</th>
<th>Delegate</th>
<th>Active</th>
<th>Action</th>
</tr>
<tr>
<td colspan="4" style="text-align:center;">
No Delegates were found for you. Delegates can be added by clicking the 'Add Delegate' Button.
</td>
</tr>
<tr>
<td></td>
<td>
<asp:DropDownList ID="deligvDDLRecipientName" runat="server" ClientIDMode="Static"
data-placeholder="Choose delegate…" class="chosen-single">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="deligvDDLActiveInsert" runat="server">
<asp:ListItem Selected="True">Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:Button ID="deligvAddButtonEmpty" runat="server" CommandName="Add" Text="Add Delegate" Width="90%" CausesValidation="false"
CssClass="gridActionbutton">
</asp:Button>
</td>
</tr>
</EmptyDataTemplate>
</asp:GridView>
This is my DataBound event:
protected void DelegateInfoGridView_DataBound(object sender, EventArgs e)
{
try
{
m_strUserID = CommonMethods.ParseUserID(User.Identity.Name);
//Get the Footer controls that have the new entry data
Control tFooterControls = getFooterControls(DelegateInfoGridView);
DropDownList ddlRecipientNames = tFooterControls.FindControl("deligvDDLRecipientName") as DropDownList;
m_strXmlTableData = m_pagingClient.GetAllPossibleDelegates(m_strUserID);
DataTable tdtAllDelegates = CommonMethods.ParseXML(m_strXmlTableData);
ddlRecipientNames.DataSource = tdtAllDelegates;
ddlRecipientNames.DataTextField = "RecipientName";
ddlRecipientNames.DataValueField = "RecipientID";
ddlRecipientNames.DataBind();
ddlRecipientNames.Items.Insert(0, new ListItem("", "0"));//This is needed for the jquery-chosen dropdown to add data-holder text
}
catch (Exception ex)
{
//TO DO: Response.Redirect("~/Error.aspx");
}
}
Why won't the dropdown display the items after all items are deleted but will correctly display if initially there are no items in the gridview to display?
I tried triggering an update for the chosen dropdown but that is called initially, before the data is retrieved.
I don't know if there is a bug in the code-behind or do I need to add something in the javascript.
Thanks.
UPDATE
The problem is not with the chosen plugin. I removed the attribute from the DropDown list that changes it to a 'chosen' style and the problem still exists. So the asp:DropDownList will not populate after the user deletes all of the items in the grid. But if the grid is initialized with no items, the DropDown is correctly populated.
UPDATE
I got a suggestion to use the 'RowDeleted' event to bind the dropdown. However, the event is not firing. I added the event to the markup:
onrowdeleted="DelegateInfoGridView_RowDeleted"
This is the event that is never called:
protected void DelegateInfoGridView_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
try
{
m_strUserID = CommonMethods.ParseUserID(User.Identity.Name);
//Get the Footer controls that have the new entry data
Control tFooterControls = getFooterControls(DelegateInfoGridView);
DropDownList tddlRecipientNames = tFooterControls.FindControl("deligvDDLRecipientName") as DropDownList;
m_strXmlTableData = m_pagingClient.GetAllPossibleDelegates(m_strUserID);
DataTable tdtAllDelegates = CommonMethods.ParseXML(m_strXmlTableData);
tddlRecipientNames.DataSource = tdtAllDelegates;
tddlRecipientNames.DataTextField = "RecipientName";
tddlRecipientNames.DataValueField = "RecipientID";
tddlRecipientNames.DataBind();
tddlRecipientNames.Items.Insert(0, new ListItem("", "0"));//This is needed for the jquery-chosen dropdown to add data-holder text
}
catch (Exception ex)
{
//TO DO: Response.Redirect("~/Error.aspx");
}
}
What is different about the RowDeleted event that it will not fire?
The problem was that I was checking if the FooterControl was null. If it was, I got the EmptyTemplateData control. When you delete all of the rows, the FooterControl is not null, but it needs to get the EmptyTemplateData control.
So, I changed the logic to check for Grid Row Count > 0 instead of a null FooterControl.
That fixed the problem..
Related
I have a dynamic grid view with different controls that added dynamically to this grid ,and one of them is drop-down list control and the data source for this drop-down list is sql table ,
My request is: I need to add a chosen plugin to ddl control that is created dynamically whit in each grid view row created ,I can easily deal with another gridview footer with below code:
$('#<%=gv_employee_Entity.FooterRow.Cells[6].FindControl("ddl_full_Crs_name").ClientID%>').chosen();
but I can't do that with a dynamic created data row
button and textbox to add gridview rows:
<asp:TextBox ID="txt_no_of_records" runat="server"></asp:TextBox>
 
<asp:Button ID="btn_add_rows" runat="server" Text="Add Rows" OnClick="btn_add_rows_Click" />
my grid view:
<asp:GridView ID="gv_employees" runat="server" AutoGenerateColumns="false" CellPadding="5">
<Columns>
<asp:TemplateField HeaderText="SL NO.">
<ItemTemplate>
<asp:TextBox ID="txt_emp_id" runat="server" Text="<%#Container.DataItemIndex + 1 %>"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="employee name">
<ItemTemplate>
<asp:TextBox ID="txt_emp_name" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Title">
<ItemTemplate>
<asp:TextBox ID="txt_emp_job_title" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Gender">
<ItemTemplate>
<asp:TextBox ID="txt_emp_gender" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
<asp:TextBox ID="txt_note" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Directorate Name">
<ItemTemplate>
<asp:DropDownList ID="ddl_direct_name" AppendDataBoundItems="true" runat="server" DataSourceID="lds_direct_name" DataTextField="Direct_name" DataValueField="Direct_name">
<asp:ListItem Text="select value"></asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource runat="server" EntityTypeName="" ID="lds_direct_name" ContextTypeName="TrainingManagementSystem.Models.emp_trainingDataContext" Select="new (Direct_name)" TableName="tbl_Directorates"></asp:LinqDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind button click event to add rows:
protected void btn_add_rows_Click(object sender, EventArgs e)
{
AddRowsToGrid();
}
protected void AddRowsToGrid()
{
List<int> noofrows = new List<int>();
int rows = 0;
int.TryParse(txt_no_of_records.Text.Trim(), out rows);
for (int i = 0; i < rows; i++)
{
noofrows.Add(i);
}
gv_employees.DataSource = noofrows;
gv_employees.DataBind();
if (gv_employees.Rows.Count > 0)
Panel1.Visible = true;
else
Panel1.Visible = false;
}
I am trying to close modelpopupextender using a button click, this button belongs to another form so modelpopupextender is not able to accessable.
I have tried to remove the updatepanel as suggested, but it's not working.
This is my form1.aspx (which includes a button, onclick which will display a ModalPopupExtender in which includes grid view)
<asp:Button id="btnclick" text="Show Modal Popup" runat="server" >
</asp:Button>
<cc1:ModalPopupExtender ID="ModelPopupExtender1" runat="server"
PopupControlID="pc" TargetControlID="btnclick" OkControlID ="Button2"
CancelControlID="Button1" BackgroundCssClass="bcc" BehaviorID="MPE1">
</cc1:ModalPopupExtender>
<asp:Panel ID="pc" runat="server" CssClass="pop" Style="display:none;">
<iframe style="width:1003px;height:550px;" id="if"src="EstPopUp.aspx" runat="server"></iframe>
</asp:Panel>
This is my EstPopUp.aspx ( Where gridview is dislpayed with save and cancel buttons)
<asp:GridView ID="gvContacts" runat="server" AutoGenerateColumns="false" CellPadding="5" DataKeyNames="EstimateBreakUpTypeId" OnSelectedIndexChanged="gvContacts_SelectedIndexChanged" OnRowDataBound="gvContacts_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="DispOrder">
<ItemTemplate>
<center>
<asp:TextBox ID="TextBox1" width="20px" runat="server" Text='<%#
Bind("DispOrder") %>'></asp:TextBox>
</center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EstBreakUpName">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Percentage(%)">
<ItemTemplate>
<center>
<asp:TextBox ID="TextBox2" Width="30px" runat="server"></asp:TextBox>
</center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" CssClass="sum" ></asp:TextBox>
<cc1:FilteredTextBoxExtender ID="fte" runat="server" Enabled="true"
TargetControlID="TextBox3" FilterType="Numbers,Custom" ValidChars=".">
</cc1:FilteredTextBoxExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br/>
<asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click"></asp:Button>
<asp:Button class="abc" ID="Button2" runat="server" Text="Cancel" CssClass=".cancel" OnClick="Button2_Click1"></asp:Button >
<br />
</asp:GridView>
My expected result is, when cancel button is clicked the modelpopup should disappear.
Try this, it works fine.
protected void Button2_Click(object sender, EventArgs e)
{
string url = " form1.aspx";
Response.Write("<script>top.location='" + url + "';parent.location='" + url + "';</script>");
}
hi i have a gridview in my asp.net webpage that is bounded after a user selects 3 dropdown list.
dropdown list's auto postbacks are set true , now my problem is when i put a button , imagebutton or linkbutton in my gridview ItemTemplate the onclick event dosent fire !
here is my code
<asp:GridView ID="GridView1" runat="server" Width="90%" Height="100%" OnRowCommand="GridView1_RowCommand" OnPageIndexChanging="GridView1_PageIndexChanging" style="margin:20px; vertical-align:top;" PageSize="10" AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="WFStudentID" ShowHeader="true" BorderWidth="1px">
<Columns>
<asp:TemplateField ShowHeader="true">
<ItemTemplate >
<tr>
<td>
<asp:Label ID="Label1" ForeColor="Silver" Font-Size="Medium" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" ForeColor="Silver" Font-Size="Medium" Text='<%# Bind("Family") %>'></asp:Label>
</td>
<td>
<asp:HyperLink ID="HyperLink1" ForeColor="Silver" Font-Size="Medium" Target="_blank" NavigateUrl='<%# Bind("WFStudentFilePath") %>' runat="server">دانلود</asp:HyperLink>
</td>
<td>
<asp:Label ID="Label7" runat="server" ForeColor="Silver" Font-Size="Medium" Text='<%# Bind("WFStudentDate") %>'></asp:Label>
</td>
<td>
<asp:Button ID="Deny" Text="deny" OnClick="Deny_Click1" runat="server" />
</td>
<td>
<asp:ImageButton ID="accept" OnClick="accept_Click" runat="server" />
</td>
<td>
<asp:TextBox ID="des" TextMode="MultiLine" Height="100px" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Deny_Click1(object sender, EventArgs e)
{
Response.Redirect("home.aspx");
}
It is probably firing, but it will throw an exception (which your catch block ignores). The exception is because of this line:
LinkButton Link = (LinkButton)sender;
The sender is a Button, not a LinkButton, so the cast is not valid and an exception will be thrown.
you can check this::
http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html
verify your code like this
Buttons inside GridView do not respond to direct events. They have to go through GridView's RowCommand event implementation. I've seen that you have implemented OnRowCommand="GridView1_RowCommand". So now you need make following change to image button:
<asp:ImageButton ID="accept" runat="server" CommandName="Accept" />
Now look for "Accept" command name in your GridView1_RowCommand event handler.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Accept")
{
// Your code goes here
}
}
I hope this helps.
I am currently in a dilemma with my gridview not returning a label, which is within a detailsview...
My C# code is:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
// get pet number for when removing a pet from reservation
int numberSelected = -1;
String numbertxt = "-1";
GridView gv1 = (GridView)sender;
GridViewRow rvRow = gv1.Rows[gv1.SelectedRow.RowIndex];
Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");
// find selected index, and get number in column 0
// label within GridView1 within dvReservation DetailsView
numbertxt = numberLbl.Text;
...
Gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="dsObjGet"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField InsertVisible="False" ShowHeader="False">
<AlternatingItemTemplate>
<asp:Label ID="lblNumber" runat="server"
Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Label ID="lblNumber" runat="server"
Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<AlternatingItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
</asp:Label>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField SelectText="Remove" ShowSelectButton="True"
CausesValidation="False">
<ControlStyle CssClass="link" />
</asp:CommandField>
</Columns>
</asp:GridView>
When I breakpoint
Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");
the label comes out as null (numberLbl)...
The message returned from the exception is:
"Object reference not set to an instance of an object"
EDIT:
This seems to be resolved if I generate lblNumber in an external gridview (on the page) with Eval("NUMBER"), though I don't see why it doesn't work in the current GridView I was trying to work with, given that GridView1 is within a DetailsView.
You should not use the Cell Collection when using FindControl. Just use this
GridView gv1 = (GridView)sender;
GridViewRow rvRow = gv1.SelectedRow;
Label numberLbl = (Label)rvRow.FindControl("lblNumber");
I have a drop down list. On changing the index of the dropdownlist , I populate an asp.net gridview.
I have a requirement where the user should be able to remove individual rows of the gridview on the screen .
At the end of each row, I intend to have a remove button. On clicking the button the row should
disappear . But this should be only on the screen. There should be no changes done in the database.
I have my code below right now :
aspx
<table>
<tr>
<td>
<div>
<asp:Label ID="lblClient" runat="server" Text="Client :" CssClass="label" ForeColor="Black"></asp:Label>
<asp:DropDownList ID="ddlClient" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="ddlClient_SelectedIndexChanged">
<asp:ListItem Text="ALL" Value="0"></asp:ListItem>
</asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvMainLog" runat="server" Visible="true" AllowSorting="True" AutoGenerateColumns="False"AllowPaging="true">
<Columns>
<asp:BoundField DataField="Instruction" HeaderText="Instruction" />
<asp:BoundField DataField="ProviderId" HeaderText="Id" />
</Columns>
</asp:GridView>
<div>
<asp:TextBox ID="txtEditMin" runat="server"></asp:TextBox>
</div>
</td>
</tr>
</table>
aspx.cs
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
gvMainLog.DataSource = GetSetupUtility(1);
gvMainLog.DataBind();
}
In the GridView add the remove command like this
<Columns>
<asp:BoundField DataField="Instruction" HeaderText="Instruction" />
<asp:BoundField DataField="ProviderId" HeaderText="Id" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Remove"
Text="Remove" CommandArgument='<%# Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
In the action of the remove button use the DeleteRow method
void gvMainLog_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Remove")
{
var id = Int32.Parse(e.CommandArgument);
gvMainLog.DeleteRow(id);
}
}
You need somthing like this:
Use TemplateField in gridview.
<script>
function deleteRow(rowId) {
$("#" + rowId).remove();
}
</script>
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div id='<%# "myRow" + Container.DataItemIndex %>'> contents <img src="deleteImageUrl" onclick='<%# "deleteRow(\"myRow" + Container.DataItemIndex+"\")" %>'/> </div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>