I am having an issue that I am certain is easy to resolve, I just don't know what to do.
Here is my code:
<asp:TemplateField>
<HeaderTemplate>
<asp:Literal ID="text_shipped" Text="Media Shipped" runat="server" />
<br />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl_shipped" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "shipped") %>' />--></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ) == "Yes" ? true : false %>' />--></ItemTemplate>
</asp:TemplateField>
The label "lbl_shipped" is showing the correct value which is either "Yes" or "No"
but, i want to add a button "lnk_ship", based on whether
or not the value is "Yes" (show button), or "No" (do not show button).
My issue is that I am using conditional code on the Visible keyword and I am testing for the value, but it seem to be ignoring my value for "shipped"
here are the main two lines, the first one shows the value, the second line is conditional, the conditional is NOT working. it keeps showing false:
<asp:Label ID="lbl_shipped" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "shipped") %>' />
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ) == "Yes" ? true : false %>' />
DataBinder.Eval(Container.DataItem, "shipped" ).ToString()
Add .ToString() ie:
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ).ToString() == "Yes" ? true : false %>' />
I have just mocked up something quickly and it is working for me (ASP.NET web forms targeting .NET 4, VS2012) , maybe have a look:
Default.aspx
Contains the following GridView definition which I stuck randomly in a new web forms project.
<asp:GridView runat="server" ID="gridMe" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Button runat="server" ID="btnName" Text="Hi" Visible='<%# DataBinder.Eval(Container.DataItem, "Name").ToString() == "Bob" %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Default.aspx.cs
Has the following class definitions
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<Thing>
{
new Thing() {ID = 1, Name = "Bob"},
new Thing() {ID = 2, Name = "Geraldine"}
};
gridMe.DataSource = list;
gridMe.DataBind();
}
}
public class Thing
{
public int ID { get; set; }
public string Name { get; set; }
}
Result
My output is something like:
ID Name
1 [Hi]
2
Related
I got a gridview where checkboxes are displayed for each and every row. Upon clicking the checkbox the alternate names label should change to textbox with the content in it remaining as it is.
If the user unchecks the checkbox the textbox should again revert back to label.
How can I achieve this in Jquery.
The code behind code is as follows:
<asp:GridView ID="GridView1" runat="server" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" OnClick="checkboxing()" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City Name Id">
<ItemTemplate>
<asp:Label ID="NameId" runat="server" Text='<%# Eval("Name_Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="CityName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Geo Name">
<ItemTemplate>
<asp:Label ID="GeoName" runat="server" Text='<%# Eval("geoname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ascii Name">
<ItemTemplate>
<asp:Label ID="AsciiName" runat="server" Text='<%# Eval("asciiname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Alternate Names" SortExpression="AlternateNames">
<EditItemTemplate>
<asp:Label ID="AlternateNames" runat="server" Text='<%# Eval("alternateNames") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="AlternateNames" runat="server" Text='<%# Eval("alternateNames") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Longitude">
<ItemTemplate>
<asp:Label ID="Longitude" runat="server" Text='<%# Eval("longitude") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Latitude">
<ItemTemplate>
<asp:Label ID="Latitude" runat="server" Text='<%# Eval("latitude") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The Javascript that executes on checking/unchecking the checkbox is the following one:
function checkboxing() {
alert("im here in checkboxing");
$("input:checkbox").click(function () {
if ($(this).is(":checked")) {
alert("This is so true ");
}
else {
alert("false");
}
});
}
Now my intention is
To change the alternate names - Label to Textbox with contents in it remaining as it is for the user to edit,
If the user unchecks the checkbox later, the contents should revert with textbox again changing to Label.
How can achieve this using JQuery
there are two ways to achieve it
1 dynamically create elements on each checkbox click
2 create both label and textbox but hide them on the basis of checkbox event
since you are using strongly typed views the second one should be the better option
if you use the client side input elements you could have dynamically append the element on the basis of click function.
now i would recommend the 2nd method instead of the 1st one because the 2nd one will manipulate the dom which in your case is not a necessity where as the 2nd option will only add a hidden class to your element which is less complex and also a better in terms of performance
The name should be input instead of label, use class each row item instead of id.
Suggestion : from input:checked find the parent row and find name
Demo
you can change the DOM from label to input field
Upon toggling the checkbox, you can enable/disable.
You can also set custom styles to the disabled input field like as if its a label.
Hope the idea is of any help
Try Below. I am assuming you are trying to edit the last row and you have only one checkbox each row.
$(document).ready(function () {
$("table input[type=checkbox]").on("change", function (event) {
var obj = $(event.target);
var tr = $(obj).closest("tr");
var td = $(tr).find("td:last");
if ($(obj).is(":checked")) {
var data = $(td).find("span").html();
$(td).html("<input id=none value=" + data + " />");
}
else {
var data = $(td).find("input").val();
$(td).html("<span >"+data+"</span>" );
}
});
});
Try the below codes
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
function toggleControl(chk)
{
var cell = $(chk).parent();
var lbl = $(cell).siblings().find('#lblNames');
var txtbx = $(cell).siblings().find('#txtNames');
var str;
if($(chk).is(':checked'))
{
str = $(lbl).text();
$(lbl).hide();
$(txtbx).val(str);
$(txtbx).show();
}
else {
$(lbl).show();
$(txtbx).hide();
}
}
</script>
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" OnClick="toggleControl(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Alternate Names">
<ItemTemplate>
<asp:Label ID="lblNames" ClientIDMode="Static" runat="server" Text='Hello World!'>
</asp:Label>
<input type="text" id="txtNames" style="display:none;" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Default.aspx.cs file codes:
namespace aspnetWeb
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Student> list = new List<Student>();
for(int i=1;i<=10;i++)
list.Add(new Student { ID = 1, Count = 20 });
GridView1.DataSource = list.AsQueryable();
GridView1.DataBind();
}
}
public class Student
{
public int ID { get; set; }
public int Count { get; set; }
}
}
I have a GridView below that will show some text and provide a checkbox.
<asp:GridView ID="surveyTableTest" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnSurveyID" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "survey_id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="URL">
<ItemTemplate>
<asp:Label ID="surveyURL" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "URL") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Used">
<ItemTemplate>
<asp:CheckBox ID="surveyUsed" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have a simple class which is bound to the GridView. This is populated from a SQL stored procedure.
public class surveyURL
{
public int survey_id { get; set; }
public string URL { get; set; }
public string used { get; set; }
}
How can I take the information from 'used' which is NCHAR(1) 'Y' or 'N' and turn that into a checkbox.checked result where 'used' = 'Y'
This is what I am currently doing in C#, this is enough to populate the hiddenfield and first template field. I don't know how to proceed for the checkbox.
var test = cn.Query<surveyURL>("sp_create_url", p, commandType: CommandType.StoredProcedure);
surveyTableTest.DataSource = test;
surveyTableTest.DataBind();
You can use the Checked property of CheckBox control and use the C# Conditional operator like this:-
<asp:CheckBox ID="surveyUsed" runat="server"
Checked='<%# Eval("used").ToString() == "Y" ? true: false %>' />
I get this error on this column in a `gridview' :-
<asp:TemplateField>
<ItemTemplate>
<%# Eval("title") %>
<br />
<p runat="server" id="description"><%# Eval("des") %></p>
<asp:ImageButton ID="click_like" runat="server" ImageUrl= "~/pics/like.png" Height="20px" CommandName="like" CommandArgument='<%# Eval("id") %>' BorderStyle="Outset" BorderColor="#99CCFF" Enabled='<%# Eval("like_enabled") %>' />
<asp:Label ID="lbl_like" runat="server" Text='<%# Eval("like") %>'></asp:Label>
<asp:ImageButton ID="click_dislike" runat="server" ImageUrl="~/pics/dislike.png" Height="20px" CommandName="dislike" CommandArgument='<%# Eval("id") %>' BorderStyle="Outset" BorderColor="#99CCFF" Enabled='<%# Eval("dislike_enabled") %>'/>
<asp:Label ID="lbl_dislike" runat="server" Text='<%# Eval("dislike") %>'></asp:Label>
<p runat="server" id="labels"><%# Eval("labels") %></p>
</ItemTemplate>
</asp:TemplateField>
I get this error on the line:-
<asp:ImageButton ID="click_like" runat="server" ImageUrl= "~/pics/like.png" Height="20px" CommandName="like" CommandArgument='<%# Eval("id") %>' BorderStyle="Outset" BorderColor="#99CCFF" Enabled='<%# Eval("like_enabled") %>' />
And I get this because I am binding the value of Enabled attribute from a DataTable where like_enabled column is either "true" or "false"
You're not able to bind Enabled like that, you'll have to use an RowDataBound event:
<asp:GridView id="myGrid" OnRowDataBound="myGrid_RowDatabound">
Code behind:
public void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var enabled = (bool)DataBinder.Eval(e.Row.DataItem, "like_enabled");
var click_like = e.Row.FindControl("click_like") as ImageButton;
click_like.Enabled = enabled;
}
}
The problem I'm having is the RowCommand of my GridView will not fire. I've read through millions of posts, and as a result I think I'm even more confused. So, if you can see what specifically it is I've done wrong here, please point it out.
I did ask a similar question a couple of weeks ago, but I was using a gridview nested in a datalist and using 'Include' Siblings of the EntityDataSource to display the Siblings, which are the many side of the relationship to Referral, it's fine for display, but figuring out Edit Update and Delete was nightmareish. So I've tied to simplify it, but am now stopped in my tracks because I'm losing controls somewhere in the postbacks.
I have a master page with a ContentPlaceholder:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="HomelessStudent.Web.SiteMasterPage" ViewStateMode="Inherit" EnableViewState="True" %>
<asp:ContentPlaceHolder ID="ContentPlaceHolderAgent" runat="server">
</asp:ContentPlaceHolder>
On the Agent page-
<asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolderAgent" runat="server">
<asp:Panel ID="SiblingPanel" runat="server" ViewStateMode="Enabled" Visible="True">
<asp:GridView ID="SiblingGridView" runat="server" CssClass="grid"
AutoGenerateColumns="false"
DataKeyNames="Id"
ShowFooter="true"
OnDataBound="SiblingGridView_DataBound"
OnRowEditing="SiblingGridView_RowEditing"
OnRowUpdating="SiblingGridView_RowUpdating"
OnRowCommand="SiblingGridView_RowCommand"
OnRowDeleting="SiblingGridView_RowDeleting" ViewStateMode="Enabled" ClientIDMode="Static">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="SiblingName">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Bind("SiblingName") %>' ID="TextBox1"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("SiblingName") %>' ID="Label1"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="NewSiblingName" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="false">
<EditItemTemplate>
<asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="UpdateLinkButton" ></asp:LinkButton> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" ID="LinkButton2"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditLinkButton" ClientIDMode="Static"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton runat="server" CommandName="AddNew" Text="Add" ID="AddNewLinkButton"></asp:LinkButton> </FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowHeader="True" HeaderText="Delete"></asp:CommandField>
</Columns>
</asp:GridView>
</asp:Panel>
Code behind (binding data here because show only those siblings that belong to a specific record, ID (Guid) for that record is in the queryString, maybe...
if (IsPostBack == false)
{
if (Request.QueryString["id"] == null)
{
if (Session["studentid"] == null)
{
Response.Redirect("StudentPage.aspx");
}
else
{
referral = GetMostRecentReferral((String)Session["studentid"]);
PopulateUI(referral);
}
}
else
{
referral = GetThisReferral(Request.QueryString["id"]);
PopulateUI(referral);
}
}
Then, in PopulateUI(referral)
some stuff...
FillSiblingGrid();
String studentId = ((referral.StudentID).ToString()).Trim();
getSelectedStudentDeatails(studentId);
Session["referralid"] = (Guid)referral.Id;
And fill siblings grid-
private void FillSiblingGrid()
{
if (referral != null)
{
List<Sibling> siblings = new List<Sibling>();
using (HomelessStudentDataEntities db = new HomelessStudentDataEntities())
{
siblings = (from s in db.Siblings
where s.ReferralID == referral.Id
select s).ToList();
}
if (siblings.Count > 0)
{
SiblingGridView.DataSource = siblings;
SiblingGridView.DataBind();
}
else
{
int TotalColumns = SiblingGridView.Rows[0].Cells.Count;
SiblingGridView.Rows[0].Cells.Clear();
SiblingGridView.Rows[0].Cells.Add(new TableCell());
SiblingGridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
SiblingGridView.Rows[0].Cells[0].Text = "No siblings found";
}
}
}
I faced the similar problem. Enabling the grid view state resolved the problem
I have a DataList which I am binding on load, which works perfectly fine. My question is how do I make this to show records that don't have a date in database, in different text colour? here is my code:
<asp:DataList ID="dlS" runat="server" EnableViewState="false">
<ItemTemplate>
<asp:Label ID="Label" runat="server" Text='<%# Eval("Name") %>' /><br />
</ItemTemplate>
</asp:DataList>
Guid ID = (Guid)Session["ID"];
lstL = Manager.Get_ByID(ID);
if (lstLetters != null)
{
dlS.DataSource = lstL;
dlS.DataBind();
}
I'm not sure what you really mean by "records that don't have a date in database",
but if it means that those records have NULL value as date then you can rewrite the Label as below:
<asp:Label ID="Label" runat="server" Text='<%# Eval("Name") %>' ForeColor='<%# Eval("DateValue") == System.DBNull ? System.Drawing.Color.Red : System.Drawing.Color.Blue %>' />