GridView convert 'Y' or 'N' into checkbox.checked on ItemTemplate field - c#

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 %>' />

Related

how to change label to textbox in gridview on checkbox click using JQuery

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; }
}
}

How to populate gridview by comma splitted string?

I have created Gridview like below lines of code.
<asp:GridView ID="grdView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkbox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Jurisdiction">
<ItemTemplate>
<asp:Label ID="lblJurisdiction" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="License Number">
<ItemTemplate>
<asp:TextBox ID="txtLicenseNumber" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have created a method in c# like below
private void FillJurisdictionGrid(string Jurisdiction)
{
Jurisdiction = "Alaska, New York, Alabama";
/* */
}
Now I want that gridview should be populated by string Jurisdiction.
The above grid should appear as
Checkbox1 - Alaska - texbox1
Checkbox2 - Alabama - textbox2
Checkbox3 - New York - textbox3
Please help me !!!
You can use Split function to split your data based on , delimiter. I hope Jurisdiction is coming as parameter in your method and in question you are showing it just for example purpose;
private void FillJurisdictionGrid(string Jurisdiction)
{
Jurisdiction = "Alaska, New York, Alabama";
string[] jurisdictionData = Jurisdiction.Split(',');
grdView.DataSource = jurisdictionData;
grdView.DataBind();
}
Finally simply add the Text attribute in your label:-
<asp:Label ID="lblJurisdiction" runat="server" Text='<%# Container.DataItem %>' />
Also, set the AutoGenerateColumns property in gridview to turn off the auto generation of columns:-
<asp:GridView ID="grdView" runat="server" AutoGenerateColumns="false">
Try this
Jurisdiction = "Alaska, New York, Alabama";
string[] names = Jurisdiction.Split(',');
grdView.DataSource = names;
grdView.DataBind();

Bind Details View in Insert Mode

I've been searching everywhere to solve this and now it's time to ask.
Here's the details view:
<asp:DetailsView ID="dvConnPipe" runat="server" AutoGenerateRows="False" ForeColor="#333333"
GridLines="None" Width="100%">
<Fields>
<asp:TemplateField HeaderText="Connection Pipe 1" SortExpression="ConnectionPipe1">
<EditItemTemplate>
<asp:TextBox ID="txtConnectionPipe1" runat="server" Text='<%# Bind("ConnectionPipe1") %>'
onkeydown="return FloatOnly(event)"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtConnectionPipe1" runat="server" Text='<%# Bind("ConnectionPipe1") %>' onkeydown="return FloatOnly(event)"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ConnectionPipe1") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="50px" />
</asp:TemplateField>
</Fields>
</asp:DetailsView>
And a dropdown list selected index changed:
protected void ddlFormItem_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlFormItem = (DropDownList)sender;
if (ddlFormItem.SelectedValue != "-1")
{
int CopyID = int.Parse(ddlFormItem.SelectedValue);
//SetInsertMode(); //If insert mode, does not bind
//SetReadOnlyMode(); //If read only, it binds OK
BindViews(CopyID);
}
}
private void BindViews(int CopyID)
{
DataTable dt = BLL.SOCBll.GetConditions(CopyID);
dvConnPipe.DataSource = dt;
dvConnPipe.DataBind();
//More views below
}
It must be possible to bind a details view while in insert mode.
If I change to read only, it binds OK.
I do not want to find each control and set it that way.
Well, after a lot of searching I did discover that you can NOT bind a detail view while in insert mode. For all the views, I had to find each control and set the default values.

Update gridview row add the old value and the new

I have a Gridview and inside I have another one nested GridView. When I press a plus button the nested GridView expands using a JavScript. The nested GridView expands on edit mode using TextBox controls. So when the user types on a TextBox would have the ability to update the cell using an update button. My problem is that when I press the update button the update occurs but not how I would expected. If for example the initial value of a cell was “My name is Peter” and I have done the edit “I don’t have a name” The new value that will be saved is exactly this: “My name is Peter, I don’t have a name”. The databind of the nested GridView occurs on the parent GridView DataBound event.
My code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="gridView_PageIndexChanging"
AutoGenerateColumns="False" DataKeyNames="myitemID"
OnRowDataBound="GridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="../plus.png" />
<asp:GridView ID="nestedGridView" runat="server"
AutoGenerateColumns="False"
DataKeyNames="mynestedID">
<Columns>
<asp:TemplateField HeaderText="nestedID" Visible="false" ItemStyle-Width="20%"
SortExpression="nesteditemID">
<ItemTemplate>
<asp:Label ID="nesteditemID" runat="server" Text='<%# Bind("nesteditemID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="20%"
SortExpression="Name">
<ItemTemplate>
<asp:TextBox ID="name" TextMode="MultiLine" Width="80%" Rows="3" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:Panel ID="mypanel" runat="server">
<table>
<tr>
<td>
<asp:ImageButton ID="ImageButton2" OnClick="updatename_Click" ImageUrl="~/images/update.jpg" Width="15px" Height="15px" runat="server"></asp:ImageButton>
</td>
</tr>
</table>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="myitemID" InsertVisible="False"
SortExpression="myitemID" Visible="False">
<ItemTemplate>
<asp:Label ID="myitemID" runat="server" Text='<%# Bind("myitemID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ItemName" ItemStyle-Width="20%"
SortExpression="ItemName">
<ItemTemplate>
<asp:Label ID="ItemName" runat="server" Text='<%# Bind("ItemName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
cs code:
protected void updatename_Click(object sender, EventArgs e)
{
GridViewRow masterrow = (GridViewRow)(sender as Control).Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent.Parent;
int index = row.RowIndex;
int mi = masterrow.RowIndex;
int i = index;
GridView nestedGridView = (GridView)GridView1.Rows[mi].FindControl("nestedGridView");
Label nestedID = (Label)nestedGridView.Rows[index].FindControl("nestedID");
int sbid = Convert.ToInt32(nestedID.Text);
TextBox name = (TextBox)nestedGridView.Rows[index].FindControl("name");
string myname = Convert.ToString(name.Text);
//update name with the new value
Nesteditem updatenesteditem = mylinqobjects.Nesteditems.Single(p => p.nesteditemID == sbid);
if (!string.IsNullOrEmpty(myname))
{
updatenesteditem.nesteditemName = myname;
mylinqobjects.SubmitChanges();
}
}
Replaced the current text by removing the old one.
string myname = name.Text.Substring(name.Text.LastIndexOf(",")+1);
Tried all possiblities, but due nested grid view rendering and its restrictions, we could do only like above.
Any others solutions, please provide.

Conditional Eval in Gridview Template row

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

Categories

Resources