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();
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 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.
I'm having problems with a web app. I have a gridview connected with SQL datatable and in the footer I have textboxes (or dropdownlists) and an "insert" button. The problem occurs when I click on the button, and want to insert new line in my SQL table. It does not read text (we need to put in some strings) from textboxes. Of course insert does not happen, since some of the data must not be null.
I have the same problem with Editing rows.
this is my code:
Gridview:
<asp:GridView ID="gvUser" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" OnRowDataBound="gvUser_RowDataBound"
OnRowCommand="gvUser_RowCommand" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last name">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%# Bind("lname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLname" runat="server" Text='<%# Bind("lname") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewLname" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<FooterTemplate>
<asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False" CommandName="Insert"
Text="Shrani"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void BindGV()
{
//bind the SQL table to the GridView (no problem here)
}
protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
{
//here I bind the dropdownlist (did not include it in code snippet,
//since firstly I need to get text from textboxes
}
protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
TextBox txtNewName = (TextBox)gvUser.FooterRow.FindControl("txtNewName");
TextBox txtNewLname = (TextBox)gvUser.FooterRow.FindControl("txtNewLname");
string NewName = txtNewName.Text; //this strings come up empty (just "")
string NewLname = txtNewLname.Text; //it should read from textboxes
AddRow(NewName, NewLname);
BindGV();
}
}
private void AddRow(string name, string lname)
{
//insert row into SQL datatable
}
EDIT:
Well it works now. Found a simmilar problem, and the author said he was able to get it work with adding EnableViewState="false" to the Gridview.
I tried and it worked. :)
Anyone here able to answer why would this work? And how will this correspond with other gv functions?
Try with this code - based on e.Item.FindControl
if (e.CommandName.Equals("Insert"))
{
TextBox txtNewName = (TextBox)e.Item.FindControl("txtNewName");
TextBox txtNewLame = (TextBox)e.Item.FindControl("txtNewLname");
....
}
I have a GridView with the following columns
<asp:TemplateField HeaderText="Name">
<FooterTemplate>
<asp:TextBox ID="txt_Name" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lbl_name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "t_Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_name" runat="server" Width="100px" Text='<%#DataBinder.Eval(Container.DataItem,"t_Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created By">
<ItemTemplate>
<asp:Label ID="lbl_tabcreatedby" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "t_CreatedBy") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Modify" ShowEditButton="True" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
<asp:TemplateField HeaderText="Add a New Name">
<FooterTemplate>
<asp:LinkButton ID="lnkbtn_AddName" runat="server" CommandName="Insert">Add Name</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
And then in the Code Behind I am trying to access the txt_Name Textbox as
protected void gv_Name_RowCommand(object sender, GridViewCommandEventArgs e)
{
string t_Name = ((TextBox)(gv_Name.FooterRow.FindControl("txt_Name"))).Text;
// Insert Code
}
But I am getting null in the string t_Name everytime irrespective of what is the current Text of txt_Name.
However I can get the text if I disable the ViewState for the page. Any explanation.
I have got around this problem by using an additional variable, see the following:
Dim txtBox As TextBox = GridView1.FooterRow.FindControl("txtName")
Dim name As String = txtBox.Text
or you can try getting the textbox by column index, like following:
protected void gv_Name_RowCommand(object sender, GridViewCommandEventArgs e)
{
string t_Name = ((TextBox)(gv_Name.FooterRow.Cells[5].FindControl("txt_Name"))).Text;
// Insert Code
}
Try following code in gv_Name_RowCommand event
if (e.CommandName.Equals("Insert"))
{
string t_Name = ((TextBox)(gv_Name.FooterRow.FindControl("txt_Name"))).Text;
}
this should work
i think you data grid is being disconnected from the data source upon post back. If you are sure that data/ data value coming from db is not null.
I tried posting this before with only text...there was only one response the suggested I update onblur..which didn't seem like the best route as there could be a considerable amount of records: I have the following Gridview built during a repeater ItemDataBound event:
'>
<asp:GridView ID="gvBeneficiary" DataKeyNames="BeneficiaryKey" OnRowCommand="gvBeneficiary_RowCommand" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink runat = "server" ID="hlEditBeneficiary" NavigateUrl='<%# "~/EditBeneficiary.aspx?bk=" + HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem, "BeneficiaryKey").ToString()) %>' Text='<%#Eval("FirstName") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Relationship" HeaderText="Relationship" />
<asp:TemplateField HeaderText="Shares %">
<ItemTemplate>
<asp:TextBox ID="txtEditShares" runat="server" Text='<%#Bind("Shares") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Beneficiary Type">
<ItemTemplate>
<asp:DropDownList ID="ddlBeneficiaryType" runat="server" SelectedIndex='<%# GetSelectedBeneficiaryType(Eval("BeneficiaryType")) %>' DataSource='<%# BeneficiaryTypes %>' ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Delete" CommandName='DoDelete' CommandArgument='<%# string.Format("{0}|{1}", Eval("BeneficiaryKey"), Eval("PlanTypeKey")) %>' ID="lbDoDelete" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HyperLink ID="hlAddBeneficiary" runat="server" Text="Add Beneficiary" NavigateUrl='<%# "~/AddEditBeneficiary.aspx?PT=" + HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem, "PlanTypeKey").ToString()) + "&CPT=" + HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem, "CorePlanType").ToString()) %>'></asp:HyperLink>
This is the codebehind:
protected void rptPlanTypes_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
GridView gvBeneficiary = (GridView) e.Item.FindControl("gvBeneficiary");
gvBeneficiary.DataSource = ((PlanType) e.Item.DataItem).BeneficiaryCollection;
gvBeneficiary.DataBind();
}
I Have one onclick event from a button that I want to do all the updates from. Anyone have any idea how to drill down the the individual controls on the rows to pull the all the data at once or in a loop.
Thanks again for all of your suggestions.
Padawan
You will want to iterate over each row of the GridView and collect the data from each control in which you are interested. For example, the following snippet will grab each txtEditShares value per row.
foreach (GridViewRow row in gvBeneficiary.Rows)
{
var txtEditShares = (TextBox)row.FindControl("txtEditShares");
var shares = txtEditShares.Text;
///...
}
You'll need to put together the guts of the loop, but that's how move through each row of a GridView and grab control values. I hope it helps.