Background: The problem I have with my code is the following. Upon the first click of a button the checkbox doesn't get the checked checkboxes, but on the second click just get all the checked checkboxes.
Here is the code:
ASPX
This button execute the process..
<asp:LinkButton ID="lbDeletePerman" runat="server" OnClick="lbDeletePerman_Click">Yes</asp:LinkButton>
<code><asp:UpdatePanel ID="GVUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView CssClass="da-table" ID="Gv_A" runat="server" DataKeyNames="ID,PatientName" AutoGenerateColumns="false" OnRowCommand="Gv_Appoint_RowCommand">
<Columns>
<asp:TemplateField HeaderStyle-Font-Bold="true" HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CbE" runat="server" />
<asp:HiddenField ID="hID" Value='<%# Eval("IDENTIFICATION") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=""Date" HeaderText="Date" HeaderStyle-Font-Bold="true" HeaderStyle-VerticalAlign="Middle" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField DataField="Cat" HeaderText="Type" HeaderStyle-Font-Bold="true" HeaderStyle-VerticalAlign="Middle" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField DataField="Deleted" HeaderText="Deleted" HeaderStyle-Font-Bold="true" HeaderStyle-VerticalAlign="Middle" ItemStyle-VerticalAlign="Middle" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
C#:
protected void lbDeletePerman_Click(object sender, EventArgs e)
{
try
{
foreach (GridViewRow rowItem in Gv_Appoint.Rows)
{
CheckBox CboxElim = (CheckBox)(rowItem.Cells[0].FindControl("CbE"));
if (CboxElim.Checked)
{
LBLT.Text = "Hello"; // NO ENTERING HERE
}
}
GVUpdatePanel.Update();
} catch (Exception er){}
}
Any help would be appreciated
lbDeletePerman needs to be inside UpdatePanel together with Gv_A.
Or use the AsyncPostBackTrigger
<asp:LinkButton ID="lbDeletePerman" runat="server"
OnClick="lbDeletePerman_Click">Yes</asp:LinkButton>
<asp:UpdatePanel ID="GVUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lbDeletePerman" />
</Triggers>
<asp:GridView>...</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Related
Is it possible to hide a LinkButton inside an UpdatePanel, which is inside a GridView? or am I going about this the complete wrong way?
I want to disable the lnkDownload button when ExpenseReceipt is null in the database and display the text "No Receipt".
when I debug, lnkDownload comes back as null.
ASP.NET:
<asp:GridView ID="gvTillExpenseRegistration" runat="server" AutoGenerateColumns="False"
EmptyDataText="No expense registered today." GridLines="Horizontal" SkinID="SimpleBlackWhite"
CellPadding="10" Caption="Today's Expense Registration" OnRowCommand="gvTillExpenseRegistration_RowCommand" DataKeyNames="ExpenseID, FileName">
<Columns>
<asp:BoundField DataField="ExpenseID" HeaderText="ExpenseID" Visible="False"/>
<asp:BoundField DataField="Description" HeaderText="Type" />
<asp:BoundField DataField="TotalAmount" HeaderText="Amount" SortExpression="TotalAmount"
DataFormatString="{0:0.00}">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="RegisterDate" HeaderText="Time" DataFormatString="{0:hh:mm tt}">
</asp:BoundField>
<asp:BoundField DataField="RegisteredBy" HeaderText="User"></asp:BoundField>
<asp:BoundField DataField="FileName" HeaderText="FileName" Visible="False"/>
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="updDownload" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" CausesValidation="False" CommandName="Download" Text='Download' />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="lnkDownload" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#:
private void LoadTodaysExpensesByTill(int tillID)
{
DataTable dt = new DataTable();
dt = new TillEndOfDayDAL().GetTodaysExpensesByTillID(tillID);
pnlTillExpenseRegistration.Visible = false;
if (dt != null && dt.Rows.Count > 0)
{
gvTillExpenseRegistration.DataSource = dt;
foreach (DataRow row in dt.Rows)
{
if (row["ExpenseReceipt"] == DBNull.Value)
{
LinkButton lnkDownload = (LinkButton)gvTillExpenseRegistration.FindControl("lnkDownload");
lnkDownload.Enabled = false;
lnkDownload.Text = "No Receipt";
}
}
pnlTillExpenseRegistration.Visible = true;
}
gvTillExpenseRegistration.DataBind();
}
You can set the Text and Enabled properties of the LinkButton inline with a ternary operator.
<asp:LinkButton ID="lnkDownload" runat="server"
Text='<%# string.IsNullOrEmpty(Eval("ExpenseReceipt").ToString()) ? "No Receipt" : "Download" %>'
Enabled='<%# string.IsNullOrEmpty(Eval("ExpenseReceipt").ToString()) ? false : true %>' />
And it would be better to wrap the GridView in the UpdatePanel, not UpdatePanel inside ItemTemplates. This could give unexpected behavior.
However in your case you could remove it completely since you are adding an UpdatePanel, and then set a PostBackTrigger, making the panel useless anyway.
You need to change your code as per below
aspx
<asp:GridView ID="gvTillExpenseRegistration" runat="server" AutoGenerateColumns="False"
EmptyDataText="No expense registered today." GridLines="Horizontal" SkinID="SimpleBlackWhite"
CellPadding="10" Caption="Today's Expense Registration" OnRowCommand="gvTillExpenseRegistration_RowCommand" OnRowDataBound="GrdView_RowDataBound" DataKeyNames="ExpenseID, FileName">
<Columns>
<asp:BoundField DataField="ExpenseID" HeaderText="ExpenseID" Visible="False" />
<asp:BoundField DataField="Description" HeaderText="Type" />
<asp:BoundField DataField="TotalAmount" HeaderText="Amount" SortExpression="TotalAmount"
DataFormatString="{0:0.00}">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="RegisterDate" HeaderText="Time" DataFormatString="{0:hh:mm tt}"></asp:BoundField>
<asp:BoundField DataField="RegisteredBy" HeaderText="User"></asp:BoundField>
<asp:BoundField DataField="FileName" HeaderText="FileName" Visible="False" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="hdnExpenseReceipt" Value='<%# Eval("ExpenseReceipt") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="updDownload" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" CausesValidation="False" CommandName="Download" Text='Download' />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="lnkDownload" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
private void LoadTodaysExpensesByTill(int tillID)
{
DataTable dt = new DataTable();
dt = new TillEndOfDayDAL().GetTodaysExpensesByTillID(tillID);
pnlTillExpenseRegistration.Visible = false;
if (dt != null && dt.Rows.Count > 0)
{
gvTillExpenseRegistration.DataSource = dt;
pnlTillExpenseRegistration.Visible = true;
}
gvTillExpenseRegistration.DataBind();
}
and also created one method
protected void GrdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
HiddenField hdnExpenseReceipt = (HiddenField)e.Row.FindControl("hdnExpenseReceipt");
if (string.IsNullOrWhiteSpace(hdnExpenseReceipt.Value))
{
LinkButton lnkDownload = (LinkButton)gvTillExpenseRegistration.FindControl("lnkDownload");
lnkDownload.Visible = false;
}
}
This method will call each time when row created in gridview.
try and let me know.
Try adding an event on gvTillExpenseRegistration
<asp:GridView ID="gvTillExpenseRegistration" runat="server"
OnRowDataBound="gvTillExpenseRegistration_DataBound"
..
protected void gvTillExpenseRegistration_DataBound(object sender, EventArgs e)
{
}
As written in the title, I am trying to get a textbox value from inside a DetailsView. However, the textbox value is not in the content of the Binded data in DetailsView, so when I try to get the text with the OnCommand method, I get an HttpContext error. Secondly, I can't reach the value using its ID, possibly due to it being in DetailsView. And finally, when I put the textbox and button outside of the DetailsView, I get the value, but the button and text shouldn't show up if there is no data to show in DetailsView.
This is my DetailsView code:
<asp:DetailsView ID="dvÜrün" runat="server" GridLines="None" CssClass="table table-borderless" AutoGenerateRows="false" OnCommand="dvÜrün_ItemCommand">
<Fields>
<asp:TemplateField Visible="false">
<ItemTemplate>
<%#Eval("ÜrünID") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ÜrünAdı" HeaderText="Adı" />
<asp:BoundField DataField="ÜrünKategori" HeaderText="Kategori" />
<asp:BoundField DataField="ÜrünAçıklama" HeaderText="Açıklama" />
<asp:TemplateField HeaderText="Ücret">
<ItemTemplate>
<%#Eval("ÜrünÜcret") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
Adet:
<asp:textbox ID="Adet" runat="server" CssClass="text-center" textmode="SingleLine" type="number" min="1" max="20" Text="1"/>
<asp:Button CssClass="pull-right btn btn-success" Text="Sepete Ekle" runat="server" OnClick="sepeteEkle_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
After posting this, I found the answer immediately. This is my cs code:
public void dvÜrün_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "ürünEkle")
{
string adet = ((TextBox)dvÜrün.FindControl("Adet")).Text;
}
}
This is the DetailsView code:
<asp:DetailsView ID="dvÜrün" runat="server" GridLines="None" CssClass="table table-borderless" AutoGenerateRows="false" OnItemCommand="dvÜrün_ItemCommand">
<Fields>
<asp:TemplateField Visible="false">
<ItemTemplate>
<%#Eval("ÜrünID") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ÜrünAdı" HeaderText="Adı" />
<asp:BoundField DataField="ÜrünKategori" HeaderText="Kategori" />
<asp:BoundField DataField="ÜrünAçıklama" HeaderText="Açıklama" />
<asp:TemplateField HeaderText="Ücret">
<ItemTemplate>
<%#Eval("ÜrünÜcret") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
Adet:
<asp:textbox id="Adet" runat="server" CssClass="text-center" textmode="SingleLine" type="number" min="1" max="20" Text="1"/>
<asp:Button CssClass="pull-right btn btn-success" Text="Sepete Ekle" runat="server" CommandName="ürünEkle"/>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
I've a grid view with three columns Employee name Employee details and Employee age.
I want to add a check box at each row and after selection of each check box i want to fire a insert query associated to that employee.
Can you tell me how to add this dynamic functionality to the grid view.
also if we use <%# EVAL %> i don't know how to implement it with checkbox.
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="details" HeaderText="details"
SortExpression="details" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connApps %>"
SelectCommand="SELECT [name], [details], [age] FROM [tblA]">
</asp:SqlDataSource>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Selected item name:<br>";
foreach (GridViewRow item in GridView1.Rows)
{
CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
if (chk != null)
{
if (chk.Checked)
{
Label1.Text += item.Cells[1].Text + "<br>";
}
}
}
}
Output:
Here is an example ,
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px"
CellPadding="0" CellSpacing="0" DataKeyNames="CategoryID" Font-Size="10"
Font-Names="Arial" GridLines="Vertical" Width="40%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server"
AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
Checked='<%# Convert.ToBoolean(Eval("Approved")) %>'
Text='<%# Eval("Approved").ToString().Equals("True") ? " Approved " : " Not Approved " %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
</Columns>
<HeaderStyle BackColor="#336699" ForeColor="White" Height="20" />
For more information , check Here !
you can use TemplateFields for the GridView:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="myCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and in code behind use below code:
ClusterName = GV.Rows(1).Cells(2).FindControl("myLabelinTheGridViewTemplateField")
I have a DataGridView that has two ItemTemplate button columns that are dynamically generated. I have code-behind for the both of the buttons that fire when they are clicked, but I also need a javascript function to run when the btnInfo button is clicked
markup:
<asp:GridView ID="gridResutls" runat="server" OnRowCommand="gridResutls_RowCommand" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="strName" HeaderText="Name"/>
<asp:BoundField DataField="strBrand" HeaderText="Brand"/>
<asp:BoundField DataField="strServing" HeaderText="Serving"/>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo"
Text="More Info" CommandArgument='<%# Eval("strID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem"
Text="Add To Log" CommandArgument='<%# Eval("strID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>'
I have a javascript function called populateLabel() that I need to fire off when btnInfo is clicked.
Any ideas? (I know similar questions have been asked and I looked throughout the posts and tried a few things but nothing seems to work)
EDIT: Here is what the code-behind for the ItemTemplate buttons looks like
protected void gridResutls_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
// Kick out if neither of the two dynamically created buttons have been clicked
if (e.CommandName != "MoreInfo" && e.CommandName != "AddItem")
{
return;
}
// If the "More Info" buton has been clicked
if (e.CommandName == "MoreInfo")
{
// Some code
}
// If the "Add To Log" button has been clicked
if (e.CommandName == "AddItem")
{
// Some code
}
}
You can do something like this.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Button btnAdd = e.Row.FindControl("btnAdd") as Button;
btnAdd.Attributes.Add("OnClick", "populateLabel();");
}
}
markup:
<script type="text/javascript">
function populateLabel() {
alert('hi');
}
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="DSModels" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo"
Text="More Info" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem"
Text="Add To Log" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Use OnClientClick:
<asp:Button ID="btnInfo" runat="server" OnClientClick="populateLabel()" CausesValidation="false" CommandName="MoreInfo"
Text="More Info" CommandArgument='<%# Eval("strID") %>'/>
Example here.
I am trying to get a gridview to populate text from the database call to my Label as shown
The Results have been tested and are returning the correct names
protected void Page_Load(object sender, EventArgs e)
{
DataTable t = DBProductLink.ListWithOptions(ProductId, LinkType, null);
TestList.DataSource = t ;
TestList.DataBind();
}
The labels are created in the Gridview like this:
<asp:GridView ID="TestList" runat="server" OnRowDataBound="testDataBound" AutoGenerateColumns="false" DataKeyNames="Id">
<Columns>
<asp:TemplateField HeaderText="Sizes">
<asp:ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I then tried to loop through the gridview and access the label using the ondatarowbound, however in this it is null.
protected void testDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
Label sizeLabel = e.Row.FindControl("sizeLabel") as Label;
sizeLabel.Text = "test";
}
I am using the exact same set up with 2 drop boxes and 2 labels on another gridview with a different name on the same page which is not having this problem. Anyone got an idea on this?
The other Gridview is as follows:
<asp:GridView ID="SearchList" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" OnRowDataBound="SearchList_RowDataBound"
OnRowCommand="SearchList_RowCommand" Width="100%" PageSize="20" >
<Columns>
<asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<ItemTemplate>
<robo:MoneyLabel ID="MoneyLabel2" runat="server"
Value='<%# Eval("Price") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label ID="typeLabel" runat="server" Text='<%# Eval("Type") %>' />
<asp:HiddenField ID="productId" runat="server" Value='<%# Eval("Id") %>' />
<asp:HiddenField ID="isFabric" runat="server" Value='<%# Eval("IsFabric") %>' />
<asp:HiddenField ID="isOldWizard" runat="server" Value='<%# Eval("IsOldWizard") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Options/Color/Size">
<ItemTemplate>
<asp:LinkButton runat="server" ID="GetOptions" Text="Get Options" CausesValidation="false" CommandName="Options" />
<asp:Label ID="OptionLabel" Visible="false" runat="server" Text="Option: " />
<asp:DropDownList ID="ProductOptions" runat="server" Visible="false" />
<asp:Label ID="ColorLabel" Visible="false" runat="server" Text="Color: " />
<asp:DropDownList ID="RibbonColors" runat="server" Visible="false" AutoPostBack="true" />
<asp:Label ID="SizeLabel" Visible="false" runat="server" Text="Size: " />
<asp:DropDownList ID="RibbonSizes" runat="server" Visible="false" AutoPostBack="true" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Add">
<ItemStyle Width="60px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Add" CommandName="Add" CommandArgument='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs is
protected void SearchList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
int productId = (int)SearchList.DataKeys[e.Row.RowIndex].Value;
LinkButton GetOptions = e.Row.FindControl("GetOptions") as LinkButton;
DropDownList RibbonColors = e.Row.FindControl("RibbonColors") as DropDownList;
DropDownList RibbonSizes = e.Row.FindControl("RibbonSizes") as DropDownList;
DropDownList ProductOptions = e.Row.FindControl("ProductOptions") as DropDownList;
Label typeLabel = e.Row.FindControl("typeLabel") as Label;
HiddenField isFabric = e.Row.FindControl("isFabric") as HiddenField;
HiddenField isOldWizard = e.Row.FindControl("isOldWizard") as HiddenField;
ProductType typeValue = DBConvert.ToEnum<ProductType>(typeLabel.Text);
bool isFabricValue = Convert.ToBoolean(isFabric.Value.ToString());
bool isOldWizardValue = Convert.ToBoolean(isOldWizard.Value.ToString());
}
I just found the problem
Your markup is wrong it was tricky... I admit it
This tag: <asp:ItemTemplate> should be <ItemTemplate>
Change this:
<asp:TemplateField HeaderText="Sizes">
<asp:ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</asp:ItemTemplate>
</asp:TemplateField>
Into
<asp:TemplateField HeaderText="Sizes">
<ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</ItemTemplate>
</asp:TemplateField>
This should raise an exception... but instead, the ItemTemplate was completely ignored