How to get values from templatefields in Gridview - asp.net

I have a gridview with template fields.
I want when I press the imagebutton to get the values of the Labels inside the ItemTemplate.
<asp:GridView ID="GridView1" ShowHeader="False"
GridLines="None" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table style="border: 0px solid">
<tr>
<td style="width: 220px">
<asp:Image ID="imgEmployee" BorderStyle="Solid" Height="182px" Width="235px"
ImageUrl='<%# Eval("Photo")%>'
runat="server" />
</td>
<td style="width: 600px">
<table>
<tr>
<td>
<b>ID:</b>
</td>
<td>
<asp:Label ID="lblId"
runat="server"
Text='<%#Eval("id")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Item:</b>
</td>
<td>
<asp:Label ID="lblItem"
runat="server"
Text='<%#Eval("Item")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Price:</b>
</td>
<td>
<asp:Label ID="lblPrice"
runat="server"
Text='<%#Eval("Price")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Notes:</b>
</td>
<td>
<asp:Label ID="lblNotes"
runat="server"
Text='<%#Eval("Notes")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server" Text ="1" ></asp:TextBox>
</td>
<td>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/buy.png" CommandName="AddToBasket" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In GridView1_RowCommand I have the following code
If e.CommandName = "AddToBasket" Then
Here i want to take the values of the labels

You need to attach OnRowCommand event method to the GridView first:
<asp:GridView ID="GridView1" ShowHeader="False"
GridLines="None" AutoGenerateColumns="False"
OnRowCommand ="GridView1_RowCommand"
runat="server">
You can get the GridViewRow from commandsource's NamingContainer and use FindControl method to find the Labels like below:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToBasket")
{
int id = 0;
string item = string.Empty; ;
decimal price = 0.00m;
string notes = string.Empty;
var gvRow = ((ImageButton)e.CommandSource).NamingContainer as GridViewRow;
var lblId = gvRow.FindControl("lblId") as Label;
var lblItem = gvRow.FindControl("lblId") as Label;
var lblPrice = gvRow.FindControl("lblId") as Label;
var lblNotes = gvRow.FindControl("lblId") as Label;
if (lblId != null && lblItem != null && lblPrice != null && lblNotes != null)
{
int.TryParse(lblId.Text, out id);
item = lblItem.Text;
decimal.TryParse(lblPrice.Text, out price);
notes = lblNotes.Text;
}
}
}

Related

asp.net- Highlight cell in listview

I have this piece of code which works fine and highlights the label. However, I want it to highlight the entire cell not just the label.
Any help would be appreciated!
protected void HighLight_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label TotalTimeLabel = (Label)e.Item.FindControl("TotalTime");
if (TotalTimeLabel != null)
{
Decimal Total;
if (Decimal.TryParse(TotalTimeLabel.Text, out Total) == true)
{
if (Total > (Decimal)1.5)
{
TotalTimeLabel.BackColor = System.Drawing.Color.Red;
TotalTimeLabel.ForeColor = System.Drawing.Color.Black;
}
}
}
}
}
The code for the table is below
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="HighLight_ItemDataBound" >
<LayoutTemplate>
<table cellpadding="1" class="TableCSS" runat="server" id="tblProducts">
<tr runat="server" style="background-color:lightgrey">
<th runat="server">enNotificationNoNI</th>
<th runat="server">TotalTime</th>
<th runat="server">TPTIMEIN</th>
<th runat="server">Status</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server" class="TableData">
<td>
<asp:Label ID="enNotificationNoNI" runat="Server" Text='<%#Eval("enNotificationNoNI") %>' />
</td>
<td>
<asp:Label ID="TotalTime" runat="Server" Text='<%#Eval("TotalTime") %>' />
</td>
<td>
<asp:Label ID="TPTIMEIN" runat="Server" Text='<%#Eval("TPTIMEIN") %>' />
</td>
<td>
<asp:Label ID="Status" runat="Server" Text='<%#Eval("Status") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Try this code
For entire row
Control ctrl = TotalTimeLabel.Parent.Parent;
HtmlTableRow tblrw = (HtmlTableRow)ctrl;
tblrw.BgColor = System.Drawing.Color.Red.Name;
For one cell
Control ctrl = TotalTimeLabel.Parent;
HtmlTableCell tblcl = (HtmlTableCell)ctrl;
tblcl.BgColor = System.Drawing.Color.Red.Name;
let me know whether this works :)

Taking a textbox's text in a listview

I have a ListView, and i have an asp:TextBox in it:
<table>
<asp:ListView ID="Users" runat="server"
DataKeyNames="Email" onitemediting="Users_ItemEditing"
onitemupdating="Users_ItemUpdating">
<LayoutTemplate>
<tr>
<td style=" width:34%;border-bottom:1px solid black;">Name</td>
<td></td>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("name") %>
</td>
<td>
<asp:LinkButton ID="Edit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="Update" runat="server" CommandName="Update">Update</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
</table>
when i change the text in the text box and then try to get it in the code behind like this:
protected void Users_ItemEditing(object sender, ListViewEditEventArgs e)
{
this.Users.EditIndex = e.NewEditIndex;
BindTheListView();
}
protected void Users_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
string Name=((TextBox)Users.Items[e.ItemIndex].FindControl("Name")).Text;
this.Users.EditIndex = -1;
BindTheListView();
}
*BindTheListView() Binds the listview Users, it works.
it takes the original text of the TextBox, the <%# Bind("name") %> instead of what i have just changed.
The update function is a lot longer so i put only what i think is needed. i know the Name recives the text that wast before in the textbox because the updtade function didnt work(it didnt change anything in the database) so i run debugg and i saw that Name recives what was before
How can i take the text that i have entered the textbox and not what was in it?
Thanks for the help
In User.ItemUpdating get new values from e.NewValues collection instead.
EDIT:
Use DataList instead of ListView:
<table>
<asp:DataList ID="Users" runat="server" DataKeyField="Email" OnEditCommand="Users_ItemEditing"
OnUpdateCommand="Users_ItemUpdating">
<HeaderTemplate>
<tr>
<td style="width: 34%; border-bottom: 1px solid black;">
Name
</td>
<td>
</td>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("name") %>
</td>
<td>
<asp:LinkButton ID="Edit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="Update" runat="server" CommandName="Update">Update</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
</asp:DataList>
</table>
Code behind:
protected void Users_ItemEditing(object sender, DataListCommandEventArgs e)
{
this.Users.EditItemIndex = e.Item.ItemIndex;
BindTheListView();
}
protected void Users_ItemUpdating(object sender, DataListCommandEventArgs e)
{
string Name = ((TextBox)e.Item.FindControl("Name")).Text;
}
on Page Load use
if (!IsPostBack)
{
BindTheListView();
}

How to Update a Panel Out side Repeater on ItemCommand of Repeater using Update Panel in asp.net?

I have a List of Client Displayed in Repeater. I have a Details Button in Repeater which displays the Details of Client when Clicked. For Sample now just added 'ClientName' only. *When i Click on 'Details' LinkButton in Repeater it Displays the Details of Selected Row. But, this causes FullPage Post Back! Which i want to Prevent. Just i want to Update the Panel which displays the Details when row is selected from Repeater*.
In .aspx page:
<script>
function ShowPopUp() {
var listItemsRegion = document.getElementById('popup');
popup.style.display = "block";
}
function ClosePopup() {
var listItemsRegion = document.getElementById('popup');
popup.style.display = "none";
}
</script>
<asp:Repeater ID="RepDetails" runat="server" OnItemCommand="RepDetails_ItemCommand">
<HeaderTemplate>
<table class="tabl">
<tr style="background-color: #808080; color: White">
<td class="lblCenter">
<asp:Label ID="Label4" runat="server" Text="City" Font-Bold="true" CssClass="lbl"></asp:Label>
</td>
<td class="lblCenter">
<asp:Label ID="Label3" runat="server" Text="Age" Font-Bold="true" CssClass="lbl"></asp:Label>
</td>
<td class="lblCenter">
<asp:Label ID="Label1" runat="server" Text="Gender" Font-Bold="true" CssClass="lbl"></asp:Label>
</td>
<td class="lblCenter">
<asp:Label ID="Label5" runat="server" Text="Details" Font-Bold="true" CssClass="lbl"></asp:Label>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="<%# Container.ItemIndex % 2 == 0 ? "rowEven" : "rowOdd" %>">
<td class="lblCenter">
<asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>' /></td>
<td class="lblCenter">
<asp:Label ID="lblAge" runat="server" Text='<%#Eval("Age") %>' /></td>
<td class="lblCenter">
<asp:Label ID="lblGen" runat="server" Text='<%#Eval("Gender") %>' CssClass="lbl"></asp:Label>
</td>
<td class="lblCenter">
<asp:LinkButton ID="lblDetails" runat="server" CommandName="Display"
CommandArgument='<%#Eval("ID") %>'>Details</asp:LinkButton></td>
<asp:Label ID="rlblClientname" runat="server" Text='<%#Eval("Client") %>' Visible="false"></asp:Label>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div id="popup" style="display: none">
<asp:UpdatePanel ID="UpdatePanel6" runat="server">
<ContentTemplate>
<table width="80%" align="center">
<tr>
<td> </td>
<td width="30%"> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Label ID="Label15" runat="server" CssClass="lbl" Text="Client Code"></asp:Label>
</td>
<td>
<asp:Label ID="lblClientName" runat="server" CssClass="lbl"></asp:Label>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<input id="Button2" type="button" value="Close" onclick="ClosePopup();" class="but" /> </td>
</tr>
</table>
</ContentTemplate>
<%-- <Triggers>
<asp:AsyncPostBackTrigger ControlID="RepDetails" EventName="RepDetails_ItemCommand" />
</Triggers>--%>
</asp:UpdatePanel>
</div>
In Repeater Item Command:
protected void RepDetails_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Display")
{
LinkButton lblDetails = (LinkButton)e.Item.FindControl("lblDetails");
Label rlblClientname = (Label)e.Item.FindControl("rlblClientname");
if (lblDetails != null && e.CommandArgument != null)
{
string val = e.CommandArgument.ToString();
if (rlblClientname != null && rlblClientname.Text != string.Empty)
{
lblClientName.Text = rlblClientname.Text;
}
string scrpt = "ShowPopUp();";
Page.ClientScript.RegisterStartupScript(this.GetType(), "s", scrpt, true);
}
}
}
This causes Full Page Post Back Which i want to Prevent.Onclick of Repeater row the details must be displayed with AsynPostBack. When adding Trigger Event to 'popup' div then it say control could not be found
Help Appreciated!
Thanks!
You have one of two Options:
1) Uncomment this code and change EventName="RepDetails_ItemCommand" to EventName="ItemCommand"
<Triggers>
<asp:AsyncPostBackTrigger ControlID="RepDetails" EventName="ItemCommand" />
</Triggers>
2) Put the Repeater in the <ContentTemplate> of the UpdatePanel

How to group one row values in Listview

I have a dynamic Listview which is bind to three different tables with one to many relations i.e. one table row may contain many rows in other table. When i run my application i get this output.
But i want to get Listview in this format although this image has been edited using Photshop.
Here is Listview HTML.
<asp:ListView runat="server" ID="LV_ViewQuestion" DataKeyNames="UID, Question_ID">
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>No Surveys.</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="SURVEY_TYPELabel" runat="server" Text='<%# Eval("Survey_Type")%>' />
</td>
<td>
<asp:Label ID="SURVEY_TITLELabel" runat="server" Text='<%# Eval("Survey_Title") %>' />
</td>
<td>
<asp:Label ID="Question_TextLabel" runat="server" Text='<%# Eval("Question_Text")%>' />
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Option_Text")%>' />
</td>
<td>
<asp:LinkButton runat="server" ID="lb_DelQuestion" Text="Delete" CommandArgument='<%# Eval("Question_ID")%>' CommandName="XDelQuestion" CssClass="GeneralInput" />
<asp:LinkButton runat="server" ID="lb_AddMoreQuest" Text="Add Question" CommandArgument='<%# Eval("UID")%>' CommandName="XAddAnotQuestion" CssClass="GeneralInput" />
<asp:LinkButton runat="server" ID="lb_Publish" Text="Publish" CommandArgument='<%# Eval("UID")%>' CommandName="XPublishSurvey" CssClass="GeneralInput" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer" runat="server" class="nobordered" style="width: 580px;">
<tr id="Tr2" runat="server" style="">
<th id="Th1" runat="server">Type</th>
<th id="Th2" runat="server">Title</th>
<th id="Th6" runat="server">Question</th>
<th id="Th4" runat="server">Options</th>
<th id="Th3" runat="server" style="width: 200px;">Actions</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" ButtonCssClass="GeneralButton" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
You can accomplish this hiding the cells in the ItemDataBound event of the ListView. Your code should look like this:
first add three global properties in your page
string type = string.Empty;
string title = string.Empty;
string question = string.Empty;
Then add the OnItemDataBound event to your list view
protected void LV_ViewQuestion_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label SURVEY_TYPELabel = (Label)e.Item.FindControl("SURVEY_TYPELabel");
Label SURVEY_TITLELabel = (Label)e.Item.FindControl("SURVEY_TITLELabel");
Label Question_TextLabel = (Label)e.Item.FindControl("Question_TextLabel");
if (SURVEY_TYPELabel.Text == type && SURVEY_TITLELabel == title &&
Question_TextLabel == question)
{
SURVEY_TYPELabel.Visible = false;
SURVEY_TITLELabel.Visible = false;
Question_TextLabel.Visible = false;
// Do the same for all the other control in cells you need to hide
}
else
{
type = SURVEY_TYPELabel.Text;
title = SURVEY_TITLELabel.Text;
question = Question_TextLabel.Text;
}
}
}

Can't assign values to the textbox inside a Panel

As written in the title I have problems assigning values to the textbox inside a panel. The problem is that a button from gvAsseti doesn't show the pnlAsset (which has textboxes in it) and doesn't load the values into the textboxes.
What is strange is that code executes fine and while using debugger I was able to see that correct values are sent to the textboxes, but for some reason they aren't displayed (all I get are empty textboxes).
Here is the codefront (sorry for the length, you can skip the middle part, it has only textboxes):
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<uc:Firma ID="ucFirma" runat="server"></uc:Firma>
<asp:GridView ID="gvKontakti" runat="server" OnRowCommand="gvKontakti_RowCommand"
DataKeyNames="idKontakt">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnShowAssets" runat="server" CommandArgument='<%# Eval("idKontakt") %>'
CommandName="ShowAssets" Text="Prikaži assete" />
<asp:Button ID="btnAddAsset" runat="server" CommandArgument='<%# Eval("idKontakt") %>'
CommandName="AddAsset" Text="Dodaj asset" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="idKontakt" HeaderText="ID" Visible="false" />
<asp:BoundField DataField="Naziv" HeaderText="Naziv" />
</Columns>
</asp:GridView>
<asp:Panel ID="pnlAsset" runat="server">
<table>
<tr>
<td>
Naziv:
</td>
<td colspan="3">
<asp:TextBox ID="txtNaziv" runat="server" Width="430px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Kod 1:
</td>
<td>
<asp:TextBox ID="txtKod1" runat="server"></asp:TextBox>
</td>
<td>
Kod 2:
</td>
<td>
<asp:TextBox ID="txtKod2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Datum isteka garancije:
</td>
<td>
<asp:TextBox ID="txtGarancija" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Lokacija:
</td>
<td colspan="3">
<asp:TextBox ID="txtLokacija" runat="server" TextMode="MultiLine" Width="455px" Height="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Opis:
</td>
<td colspan="3">
<asp:TextBox ID="txtOpis" runat="server" TextMode="MultiLine" Width="455px" Height="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Aktivna imovina:
</td>
<td>
<asp:CheckBox ID="chkAktivna" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Spremi" OnClick="btnSave_Click" />
</td>
</tr>
</table>
</asp:Panel>
<asp:UpdatePanel ID="upAsseti" runat="server">
<ContentTemplate>
<asp:GridView ID="gvAsseti" runat="server" onrowcommand="gvAsseti_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnShowAsset" runat="server" CommandArgument='<%# Eval("idAsset") %>'
CommandName="ShowAsset" Text="Prikaži asset" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
And also the codebehind:
protected void gvAsseti_RowCommand(object sender, GridViewCommandEventArgs e)
{
int idFirma = Convert.ToInt32(Request.QueryString["idt"]);
int idAsset = Convert.ToInt32(e.CommandArgument);
TicketingSystemEntities db = new TicketingSystemEntities();
if (e.CommandName=="ShowAsset")
{
var asset = (from a in db.Assets
where a.idAsset == idAsset
select a).SingleOrDefault();
pnlAsset.Visible = true;
txtGarancija.Text = asset.DatumGarancije.ToString();
txtKod1.Text = asset.Kod1;
txtKod2.Text = asset.Kod2;
txtLokacija.Text = asset.Lokacija;
txtNaziv.Text = asset.Naziv;
txtOpis.Text = asset.Opis;
if (asset.Aktivan == true)
{
chkAktivna.Checked = true;
}
else
{
chkAktivna.Checked = false;
}
}
}
}
Any help would be appreciated.
Put the panel inside the updatepanel contenttemplate. As gvAsseti's inside an updatepanel, therefore it'll be refreshing the contents of the updatepanel on postback.

Resources