Label inside gridview is not showing any value - c#

Label4 inside gridview is not displaying textbox value while Label5 outside gridview is showing the textbox value. Label4 Value is blank. I want that the label i.e Label4 should display Label5.Text.
<Columns>
<asp:TemplateField HeaderText="Place Details">
<ItemTemplate>
<table>
<tr>
<td>
<b>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>&nbsp&nbsp&nbsp&nbsp&nbsp
<asp:Label ID="Label2" runat="server" Text='<%# Eval("phno") %>'></asp:Label>
</b>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("zone") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>

Using Gridview Row Databound bind value of label4
Label Label4 = (Label)e.row.findcontrol("Label4");
Label4.text = (Your Value assign here)
so display your value

I've had this issue which disappeared when I commented out everything inside my GridView1_RowDataBound(object sender, GridViewRowEventArgs e) method. I was adding a tooltip to all cells in the row which I think overwritten it.
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
// var id = e.Row.Cells[0].Text;
//if (e.Row.RowType == DataControlRowType.DataRow)
//{
}

Related

In Paged Listview CheckBox_CheckedChanged event not working

The Problem is when I check the checkbox it works great, but when I check the checkbox on next page. previous page check boxes gets unchecked.
Checked box Checked/Unchecked event fires CheckedChanged event. But when I check the checkbox in listview next page of listview it uncheck's the checkboxes of listview previous Page.
ListView.aspx Code
<table class=" example1 table table-bordered table-striped">
<thead>
<tr>
<th>Sr no.</th>
<th>Parent Category</th>
<th>Title</th>
<th>Description</th>
<th>Image</th>
<th>Show on Homepage</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<asp:ListView ID="ListCourse" runat="server" OnItemCommand="ListCourse_ItemCommand" DataKeyNames="CID">
<LayoutTemplate>
<tr id="ItemPlaceholder" runat="server">
</tr>
</LayoutTemplate>
<ItemTemplate>
<tr class="gradeA">
<td>
<asp:Label ID="lblSrno" runat="server" Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
</td>
<td>
<asp:Label ID="lbl" runat="server" Text='<%# GetCourse(Convert.ToInt32( Eval("CatID"))) %>'></asp:Label>
</td>
<td>
<asp:Label ID="Lbltitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
<asp:Label ID="lablc" runat="server" Visible="false" Text='<%# Eval("CID") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblDescrption" runat="server" Text='<%# (Eval("Description").ToString().Length <=200)?Eval("Description").ToString(): Eval("Description").ToString().Substring(0, 200) + "..."%>'></b></asp:Label>
</td>
<td>
<img class="img_show " src="/Gallery/<%# Eval("Image")%>">
</td>
<td>
<asp:CheckBox ID="CheckCourse" runat="server" Style="margin-left: 50px;" OnCheckedChanged="CheckCourse_CheckedChanged" AutoPostBack="true" />
</td>
<td>
<asp:LinkButton ID="LinkEdit" runat="server" PostBackUrl='<%# "Add_New_Course.aspx?ID="+ Eval("CID")%>'>Edit</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkDelete" runat="server" CommandName="DeleteCourse" CommandArgument='<%# Eval("CID") %>' OnClientClick='return confirm("Do you want to delete record ??")'> Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</tbody>
</table>
Code Behind CheckedChanged
protected void CheckCourse_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkhome = (CheckBox)sender;
ListViewItem item = (ListViewItem)checkhome.NamingContainer;
ListViewDataItem dataItem = (ListViewDataItem)item;
string code = ListCourse.DataKeys[dataItem.DisplayIndex].Value.ToString();
int CID = Convert.ToInt32(code);
Course_Master objnew = DB.Course_Master.Single(p => p.CID == CID);
bool IsHome = CheckOnHome(CID);
if (IsHome == true)
{
if (checkhome.Checked == false)
{
objnew.ShowOnHomePage = false;
}
}
else
{
if (checkhome.Checked == true)
{
objnew.ShowOnHomePage = true;
}
}
DB.SaveChanges();
}
It doesn't fire because when the postback fires the server doesn't know the previous state of the checkbox, so it doesn't know if it's changed or not.
Try to set the default value to false and it should work
<asp:CheckBox ID="CheckCourse" runat="server" Checked="false" Style="margin-left: 50px;" OnCheckedChanged="CheckCourse_CheckedChanged" AutoPostBack="true" />
You need to save ids somewhere of checked item from the list. As when you move to the page 2 of ListView it lost its previous state.
Store data in viewstate and load it from there. To read and to bind it, you would need to handle PagePropertiesChanging and ItemDataBound event handlers of ListView.
Here is a good explaination regarding Maintaining the state of checkboxes in ListView
Please give +1 if it helped. Cheers!
Thank you Guys for the help. I solved it using some simple procedure's by removing the check-boxes from the ListView and adding text instead of it regarding whether the checkbox is checked or not(Yes/No). As I thought it will the easiest way to solve my problem.

cannot find controls in gridview and get an error(Index was out of range)

Actually I have problem with finding LinkButton Control in gridview .
I have 2 gridviews which one of them is inside another one so my problem is I cannot get the value of LinkButton of second gridview,
here is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderStyle="None" DataSourceID="SqlDataSource2" GridLines="None">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<table align="center" class="Table3">
<tr>
<td >
<asp:Label ID="lblID" runat="server" Visible="false" Text='<%# Eval("Food_ID") %>'></asp:Label>
<b> <%#Eval("Title")%></b>
</td>
</tr>
<tr>
<td align="center" class="ImgKidFood">
<asp:Image ID="Img" Width="680px" Height="145px" ImageUrl='<%#Eval("Pictures") %>' runat="server" />
</td>
</tr>
<tr>
<td style="direction:rtl; text-align:right;">
<asp:GridView ID="ShowFoodMenu2" runat="server" AutoGenerateColumns="False"
BorderStyle="None" GridLines="None" ShowHeader="False"
DataSourceID="SqlDataSource1" Width="100%"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table align="center" class="TableListMenu">
<tr>
<td class="Add">
<asp:LinkButton ID="LinkButton2" CommandArgument="<%# Container.DataItemIndex %>"
runat="server" CausesValidation="False"
CommandName="Select" Text="Select"
onclick="LinkButton2_Click" ></asp:LinkButton>
</td>
<td class="ToCenter">
<b><%#Eval("Title_Pr") %>
</b>
<asp:LinkButton ID="LinkButton12" runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Link2" Text='<%# Eval("Menu_ID") %>'></asp:LinkButton>
</td>
<td class="PriceLeft">
<%#Eval("Price") %>
</td>
</tr>
</table>
<hr />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><br /><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
As u can see the second gidview named id is ShowFoodMenu2 and it's inside first girdview which id Giridview1 .
and also I have two Linkbuttons inside the second gridview
one of them keep the value(which is LinkButton12) and another one(LinkButton2) is for when I clicked it add a record in Database .
But when I clicked on Linkbutton(LinkButton2) to show the value of Linkbutton12 , I will get ann error
Here is the error
http://hidelion.com/Images/error.png
and here is my.cs code
protected void LinkButton2_Click(object sender, EventArgs e)
{
GridView G = new GridView();
G.FindControl("ShowFoodMenu2");
System.Threading.Thread.Sleep(1000);
LinkButton m = (LinkButton)sender;
int i = Int32.Parse(m.CommandArgument);
LinkButton LblMososID = (LinkButton)G.Rows[i].FindControl("LinkButton2");
// LinkButton LblMososID2 = (LinkButton)G.Rows[i].FindControl("LinkButton12");
Label1.Text = LblMososID.Text;
}
So how can I solved this problem ??????
GridView G = new GridView(); just create a new instance of Gridview class, and it will not find your gridview automatically, You are messing it up, simply go like this, from linkbutton find the corresponding row, from that row fetch the control you wish to manipulate, do it like this:-
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton LinkButton2 = sender as LinkButton;
GridViewRow grdRow = (GridViewRow)LinkButton2.NamingContainer;
LinkButton LinkButton12 = (LinkButton)grdRow.FindControl("LinkButton12 ");
Label1.Text = LinkButton12.Text;
}

Change header text in Rowdataboud of Gridview nested inside Repeater

I have a gridview nested inside a repeater, I want to change the header text of gridview columns on row databound or through
<HeaderTemplate>
<asp:Label runat="server" ID="lblMode" Text='<%# Eval("IsValidForPromoCode")%>'>
</asp:Label></HeaderTemplate>
whichever is convenient.
.aspx page
<asp:Repeater ID="repRequest" runat="server" OnItemDataBound="repRequest_ItemDataBound">
<ItemTemplate>
<table style="width: 100%; font-weight: bold;" cellpadding="5" cellspacing="0">
<tr>
<td colspan="12" align="right">
<a id="aSetPreference" runat="server" href="#">Attached Document(s)-</a>
<asp:Label ID="lblDocumentCount" CssClass="redFont" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td align="left" class="gray-bg" style="width: 8%;">
<b>Request#:</b>
</td>
<td style="width: 100px;">
<span class="detail-info-color">
<%# Eval("RequestNumber")%></span>
</td>
<td align="left" class="gray-bg" style="width: 5%;">
<asp:Label ID="lblreqDetID" runat="server" Visible="false" Text='<%# Bind("TravelDetailsID") %>'></asp:Label>
<b>Date:</b>
</td>
<td align="left" class="gray-bg" style="width: 5%;">
<b>Class:</b>
</td>
<td>
<span class="detail-info-color">
<%# Eval("Class")%></span>
</td>
</tr>
<tr>
<td colspan="12">
<asp:GridView ID="gvOption" CssClass="gridRow" runat="server" AutoGenerateColumns="False"
Width="100%" OnRowDataBound="gvOption_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Service Provider">
<ItemTemplate>
<asp:Label ID="txtNumber" runat="server" Width="80px" Text='<%# Bind("Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblMode" Text='<%# Eval("IsValidForPromoCode") %>'></asp:Label></HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtName" runat="server" Width="100px" Text='<%# Bind("Name") %>'></asp:Label>
<asp:Label ID="lblOptionID" Visible="false" runat="server" Width="100px" Text='<%# Bind("optionID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Code behind
protected void gvOption_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
GridView gvOption = (GridView)sender;
if (e.Row.RowType == DataControlRowType.DataRow)
{
gvOption.Columns[1].HeaderText = "Last Name";
}
if (e.Row.RowType == DataControlRowType.Header)
{
gvOption.Columns[2].HeaderText = "Last Name";
}
}
catch (Exception ex)
{
throw ex;
}
}
Whichever way I am doing it's not effecting. Please suggest what I am missing.
Try this:-
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].Text = "Last Name";
}
Here, I have hard-coded the Cells value you need to change it accordingly.
Update:-
Find Control inside RowDataBound:-
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label txtNumber = (Label)e.Row.FindControl("txtNumber");
txtNumber.ForeColor = System.Drawing.Color.Red;
}

Repeater inside another repeater. access an event values

I have an repeater inside another repeater. The second repeater have an checkbox event "isChecked_OnCheckedChanged". My problem is there. When this event occurs I need to access the value of the variable "lblName" and also the value of the variable "lblID" in parent repeater.
<asp:Repeater ID="rptOne" OnItemDataBound="populateSecondRepeater" runat="server">
<HeaderTemplate>
<table s>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</td>
<td style="width: 15%; vertical-align: top;">
<asp:Repeater ID="rptTwo" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width: 85%;">
<img runat="server" src='<%# Eval("Img") %>' alt="#" id="img_flag" /></td>
<td style="width: 15%; padding-right: 40px">
<asp:Label ID="lblName" runat="server" Style="display: none;" Text='<%# Eval("IDName") %>'></asp:Label>
<asp:CheckBox ID="check" runat="server" AutoPostBack="True" Checked='<%# Eval("isChecked") %>' OnCheckedChanged="isChecked_OnCheckedChanged" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I can access the value of the variable "lblName", but how do I access the values ​​of the parent repeater?
protected void isChecked_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (sender as CheckBox);
RepeaterItem item = chk.NamingContainer as RepeaterItem;
if (item != null)
{
Label lbl = (Label) item.FindControl("lblName");
}
}
thank you.
There are multiple ways to do this. You can add something like ParentID property to your data model, set that to the value you want and bind that to a hidden field next to the lblName label.
Alternatively, use a <tr runat="server" id="rptOneRow"> for each item in rptOne, and jump up in isChecked_OnCheckedChanged from the RepeaterItem until you find such row. Then use FindControl to find the lblID.
Or see here how to access the data directly - Accessing parent data in nested repeater, in the HeaderTemplate.

Keeping prior values when using listview insert

Currently I have a gridview that when I click on select it populates values in a listview.
I handle this in code behind using the selectedindexchanged event. And I even populate a text box in the insertitem template for new entries.
protected void GridView9_SelectedIndexChanged(object sender, EventArgs e)
{
// this handles the transmittal costs
SqlDataSource29.SelectParameters.Clear();
SqlDataSource29.SelectParameters.Add("tc_t_id", App_id);
SqlDataSource29.InsertParameters.Clear();
ListView1.Visible = true;
ListView1.DataBind();
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = App_id;
However the issue arises when I do an insert. I lose value I put into the listviews insertitem texbox tc_t_idTextBox. Actually I lose the value when I edit and delete also.
There must be a way to hold onto that value between inserts.
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
</td>
<td>
<asp:TextBox ID="tc_dateTextBox" runat="server" Text='<%# Bind("tc_date") %>' />
</td>
<td>
<asp:TextBox ID="tc_costTextBox" runat="server" Text='<%# Bind("tc_cost") %>' />
</td>
<td>
<asp:TextBox ID="tc_typeTextBox" runat="server" Text='<%# Bind("tc_type") %>' />
</td>
<td>
<asp:TextBox ID="tc_commentTextBox" runat="server" Text='<%# Bind("tc_comment") %>' />
</td>
<td>
</td>
<td>
<asp:TextBox ID="tc_t_idTextBox" runat="server"
Text='<%# Bind("tc_t_id") %>' Enabled="false" Width="15" />
</td>
</tr>
</InsertItemTemplate>
By using DataBind() on every PostBack, the ListView is being reloaded every time the user performs and action. In the Page_Load of your page, do this instead:
SqlDataSource29.SelectParameters.Clear();
SqlDataSource29.SelectParameters.Add("tc_t_id", App_id);
SqlDataSource29.InsertParameters.Clear();
ListView1.Visible = true;
if(!IsPostBack)
{
ListView1.DataBind();
}
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = App_id;

Categories

Resources