ASP.NET C# DataList - Find Hidden Field in Child DataList - asp.net

I am new to ASP.NET. I have been trying all morning, but couldn't get this to work. In my DataList1, there is a child dataList (childList), which lists the Company names and checkboxes. I also stored the CompanyID in the hidden field (hiddenCompanyID).
What I like to do is when the user checkes a boxes (under child Datalist), it will trigger the event (chkChildCompany_CheckedChanged) and capture the CompanyID from the hidden field so I can process the database update. I could not figure how to find the Hidden field control with in the child Datalist.
Please help,
Thanks,
<asp:DataList BackColor="#ffffff" id="DataList1" DataSourceID="dsCompanyListPartialMatch" runat="server" Width="80%" DataKeyField="Company1Word"
UseAccessibleHeader="true"
CssClass="books"
HeaderStyle-CssClass="header"
ItemStyle-CssClass="item"
AlternatingItemStyle-CssClass="alternating"
GridLines="Both"
CellPadding="0"
CellSpacing="0" BorderColor="Black"
ItemStyle-BorderColor="Black" BorderWidth="0"
HorizontalAlign="Center"
RepeatDirection="Vertical"
>
<HeaderTemplate>
<table border="0" width="100%">
<tr class="div_hover">
<th style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; "></th>
<th style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; ">Num</th>
<th style="width: 70%; border-right:1px solid black; border-spacing:0; text-align:center; ">Company Name</th>
<th style="width: 10%; border-right:1px solid black; border-spacing:0; text-align:center; ">Add?</th>
</tr>
</table>
</HeaderTemplate>
<ItemStyle BorderColor="black" Font-Size="Medium" />
<ItemTemplate>
<table border="0" width="100%">
<tr class="div_hover">
<td style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; ">
<asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" Text="+" CommandArgument='<%#Container.ItemIndex%>'
OnCommand="LinkButton1_Command"
Font-Underline="false"
Height="25"
Font-Bold="true"
></asp:LinkButton>
</td>
<td style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:right; padding-right:10px;"><%#Eval("Row")%></td>
<td style="width: 70%"><asp:Literal ID="litFoo" runat="server" Text='<%#Eval("Company")%>' /> </td>
<td style="width: 10%;text-align:right;">
<div style="<%# (Eval("CompanyID") == null || Eval("CompanyID").ToString() == "") ? "": "display: none" %>">
<asp:CheckBox id="check1" runat="server" />
</div>
<div style="<%# (Eval("CompanyID") == null || Eval("CompanyID").ToString() != "") ? "": "display: none" %>">
<asp:HyperLink
ID="HyperLink1"
runat="server" ForeColor="Blue"
Text='<%# Eval("CompanyID")%>'
NavigateUrl='<%# Eval("CompanyID", "/Apps/ERP/Other/CompanyInfo.asp?CompanyID={0}")%>'
/>
</div>
</td>
<asp:Label ID="lblRow" Visible="False" runat="Server" Text='<%# DataBinder.Eval(Container.DataItem, "Row") %>' />
</tr>
</table>
<asp:Panel ID="pnlChildView" runat="server" style="padding-left:200px;">
<asp:DataList ID="childList" runat="server" Width="100%">
<ItemTemplate>
<div class="div_hover">
<table class="table1" width="80%">
<tr>
<td style="width: 60%; border-right:0px solid black; border-spacing:0;">• <%#Eval("CompanyName")%></td>
<td style="width: 20%;text-align:right; "><a href="/Apps/ERP/Other/CompanyInfo.asp?CompanyID=<%#Eval("CompanyID")%>" ><%#Eval("CompanyID")%></a></td>
<td style="width: 20%;text-align:right;">
<asp:CheckBox id="chkChildCompany" runat="server" value="123Test"
AutoPostBack="true"
OnCheckedChanged="chkChildCompany_CheckedChanged" /></td>
<asp:HiddenField ID="hiddenCompanyID" runat="server" Value='<%#Eval("CompanyID") %>' />
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
checkbox event
protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
{
//Process through child DataList (childList)
//Capture the CompanyID from hidden field value if checkbox is checked;
// Then I will update the database record with the ID....
}

A couple different ways to tackle this. My first instinct is to cast sender to the appropriate type and then check the .Parent property. That will probably allow you to do this:
HiddenField hidden = (HiddenField) myCheckbox.Parent.FindControl("hiddenCompanyID");
If that doesn't work... another option would be to embed a postback reference that already includes the appropriate hidden field value, which is triggered by the checkbox. Or modify things slightly so that you've got a button there, which accepts a CommandArgument param. In this case you'd just set it to <%#Eval("CompanyID") %>, and then that would be available in the event handler.

I would add an attribute to your checkbox, and then obtain the value you need from the checkbox itself.
<asp:CheckBox id="chkChildCompany" runat="server" value="123Test"
AutoPostBack="true" CustomAttribute='<%#Eval("CompanyID") %>'
OnCheckedChanged="chkChildCompany_CheckedChanged" />
CodeBehind:
var CompanyID = (sender as CheckBox).Attributes["CustomAttribute"]

You can use the NamigContainer property to get the DataListItem, then use FindControl:
protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
{
var chk = (CheckBox) sender;
var item = (DataListItem) chk.NamingContainer;
var hiddenCompanyID = (HiddenField) item.FindControl("hiddenCompanyID");
// here you are ...
}
By the way, this technique works in all web-databound controls like Repeater, GridView etc. Using the NamingContainer is safer than using Parent.Parent or other approaches.

In your case, you should store the CompanyID in ViewState instead of a hidden field:
ViewState["MicrosoftID"] = "4536";
Later, you can access it like this:
protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
{
string companyID = ViewState["MicrosoftID"];
}
Hope that helps!

Related

Style table in repeater from code behind

Hey guys I need some help with an application that I'm developing.
I'm trying to add a css class / styling to a table depending on a certain value that I'm getting from the database ( e.g. 0 - 2).
This is the code where I need to change the styling of the table
Public Function projectType(ByVal value As Integer)
Dim projectName As String
If value = 0 Then
projectName = "Project"
mytable.AddAttributes("Style", "Background-color:#444444")
ElseIf value = 1 Then
projectName = "Support"
Else
projectName = "Not available"
End If
Return projectName
End Function
Markup:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="1"
RepeatDirection="Vertical" CellPadding="0" CellSpacing="0">
<ItemTemplate>
<table cellpadding="0" cellspacing="0" style="width: 100%; text-align: left; border-bottom: 1px solid #999999;
padding-bottom: 15px;">
<tr>
<td style="width: 110px; vertical-align: top; padding-left: 20px; padding-top: 5px;
padding-bottom: 5px;">
<asp:Label ID="Label5" runat="server" Text='<%# projectType(Eval("Type")) %>' Font-Names="Verdana"
Font-Size="9pt" EnableTheming="false" />
</td>
<td style="width: 110px; vertical-align: top; padding-left: 20px; padding-top: 5px;
padding-bottom: 5px;">
<asp:Label ID="Label2" runat="server" Text='<%# hoursCheck(Eval("Duration")) %>'
Font-Names="Verdana" Font-Size="9pt" Style="text-align: right" EnableTheming="false" />
</td>
</tr>
<tr>
<td colspan="2" style="padding-bottom: 5px; padding-top: 5px; text-align: center">
<asp:Label ID="Label7" runat="server" Text='<%# Eval("FullName") %>' Font-Names="Verdana"
Font-Size="7pt" EnableTheming="false" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
From this function I need to access the table by getting the parent table of the sender since I can't explicitly say I want Datalist1 since I have 5 of them, how can I do this?
First of all, please get rid of inline styles and replace them with CSS classes. Will save you a lot of headache later on. For instance, have two CSS classes, one for "Project", another for "Support" and "Not available"
As for the actual question, the easiest thing that comes to mind is to split the logic into two functions, one for text and one for css class:
Public Function projectTypeText(ByVal value As Integer)
...
Return projectName
End Function
Public Function projectTypeClass(ByVal value As Integer)
...
Return projectCssClass
End Function
And then use it exactly as you did:
<%-- This might actually need runat="server", not sure --%>
<table cellpadding="0" cellspacing="0" style='<%# projectTypeClass(Eval("Type")) %>'
<asp:Label ID="Label5" runat="server" Text='<%# projectType(Eval("Type")) %>'
Note: if you absolutely have to deal with inline classes, you can have projectTypeStyle which returns Background-color:#444444, but then inside the markup you will need to do nasty stuff like that:
<table cellpadding="0" cellspacing="0" style='<%# "width: 100%; rest of styles; " + projectTypeStyle(Eval("Type")) %>'
This is horrible, so please avoid at all cost.

Get fields within a panel within a repeater on button click

I have an item repeater that displays items that have been bid on by a user. The items are contained within a panel as they contain form elements to update the bid for each item individually. What I would like is for the user to be able to submit the individual item to be updated and for me to know which item they are trying to update so I can ignore all other fields when processing the update.
Right now, my repeater looks like:
<asp:Repeater ID="itemRepeater" runat="server" onitemdatabound="itemRepeater_DataBound">
<ItemTemplate>
<!-- Auction Item MASTER-->
<asp:Panel id="pnlDefaultButton" runat="server" DefaultButton="absenteeBtnBid">
<div style="position: absolute; left: 0px; top: 0px; transform: translate(0px, 236px);" class="item auction_item firearm user_item isotope-item">
<div class="item_image">
<asp:HyperLink ID="item_img_link" runat="server" Visible="False" EnableViewState="False">
<asp:Image ID="item_img" runat="server" Visible="False" EnableViewState="False" Width="224" />
</asp:HyperLink>
</div>
<div class="item_overlay">
<div class="item_buttons">
Following
<asp:Label ID="absenteeBidLabel" runat="server" Text="" CssClass="absenteePlaceBidLabel" AssociatedControlID="absenteeBid" style="font-size:11px;" Visible="false">
<asp:TextBox ID="absenteeBid" runat="server" Wrap="False" CssClass="absenteePlaceBid" placeholder="Enter Bid" />
</asp:Label>
<asp:TextBox ID="absenteeBidId" runat="server" Wrap="False" CssClass="timedPlaceBid" style="display:none;" Visible="false" />
<asp:TextBox ID="absenteeBidClose" runat="server" Wrap="False" CssClass="timedPlaceBid" style="display:none;" Visible="false" />
<asp:TextBox ID="absenteeBidSaleId" runat="server" Wrap="False" CssClass="timedPlaceBid" style="display:none;" Visible="false" />
<asp:Button runat="server" ID="absenteeBtnBid" cssClass="startbidding_button edit_button" OnClick="onclick_absenteeBid" Text="Edit Bid" />
<div class="bid_options">
Live Bid
Bid by Phone
<asp:HyperLink ID="bid_withdraw" runat="server" CssClass="withdrawbid"></asp:HyperLink>
</div>
</div>
<table class="item_bidstatus" border="0" cellpadding="0" cellspacing="0" width="190">
<tbody>
<tr>
<td class="status_label" width="50%">Bids:</td>
<td class="status_value" width="50%"><asp:Label ID="bid_count" runat="server" Text="0" /></td>
</tr>
<tr>
<td class="status_label">My Top Bid:</td>
<td class="status_value"><asp:Literal ID="bid_amount" runat="server"></asp:Literal></td>
</tr>
<tr>
<td class="status_label">Your Status:</td>
<td class="status_value status_low">LOW BID</td>
</tr>
</tbody>
</table>
<div class="item_description">
<h5>
<asp:HyperLink ID="labelLot" runat="server">Lot #<%# Eval("item_lot")%></asp:HyperLink> - <asp:HyperLink ID="item_title" runat="server"><%# Eval("item_title")%></asp:HyperLink>
</h5>
<asp:Label ID="labelEst" runat="server" Visible="false"></asp:Label>
<p class="item_details"><asp:Label ID="labelDesc" runat="server"><%# Eval("item_desc")%></asp:Label></p>
> Item Details
</div>
</div>
<table class="item_footer" width="100%">
<tbody>
<tr>
<td><div class="item_category"><asp:HyperLink ID="item_sale" runat="server"></asp:HyperLink></div></td>
<td><div class="item_daysleft">Bid left: <asp:Literal ID="bid_time" runat="server"></asp:Literal></div></td>
</tr>
</tbody>
</table>
</div>
</asp:Panel>
<!-- /Auction Item MASTER-->
</ItemTemplate>
</asp:Repeater>
So my question would be how do I make the method onclick_absenteeBid only look at the form fields within the panel where the submission was made? Or am I even going about this the right way at all using a panel within a repeater?
Thre's nothing wrong with this approach. You have to find the container panel in the button's click event and find controls inside it. Here's how you can do it:
protected void onclick_absenteeBid(object sender, EventArgs e)
{
Panel pnl = ((Button) sender).Parent as Panel;
if (pnl != null)
{
//Access controls inside panel here like this:
TextBox absenteeBidId = pnl.FindControl("absenteeBidId") as TextBox;
if(absenteeBidId != null)
{
string myAbsenteeBidId = absenteeBidId.Text;
}
//Access Repeater Item
RepeaterItem itm = pnl.NamingContainer as RepeaterItem;
if (itm != null)
{
// Do stuff
}
}
}

Delete image from folder using data list delete button

I display some images in a datalist by getting the images from folder.
Now, I want to delete the image in folder when I click the Delete button on my datalist .
Here is my delete button code:
protected void delete_onClick(object sender, EventArgs e)
{
string fileName = sender as string;
File.Delete(Server.MapPath(fileName));
FileInfo fInfo;
fInfo = new FileInfo(fileName);
fInfo.Delete();
gvImages.DataBind();
}
I don't know how to get the exact image name which I want to delete, there is a delete button with each image.
Here is my datalist:
<div>
<asp:DataList ID="gvImages" RepeatColumns="5" RepeatDirection="Horizontal" GridLines="Horizontal"
runat="server" BorderColor="#336699" BorderStyle="Solid" ShowHeader="true">
<ItemTemplate>
<center>
<table>
<tr>
<td style="width: 90px; height: 90px">
<img id="PICID" runat="server" src='<%# Container.DataItem %>' alt='' style="height: 100px;
width: 100px;" />
<br />
<asp:Button ID="Delete" Height="22px" OnClick="delete_onClick" Width="100px" runat="server"
Text="Delete Picture" /><br />
</td>
</tr>
</table>
</center>
</ItemTemplate>
</asp:DataList>
</div>
Nesting functions as you have done is a poor programming practice:
File.Delete(Server.MapPath(fileName));
Try is like this and then when you debug, you will be able to see what file you are are trying to delete:
string fileName = e.CommandArgument;
fileName = Server.MapPath(fileName);
File.Delete(fileName);
Also, are you getting an error? An exception? Why isn't there an exception handler around the code?
you should use commandName on button. And you should use OnDeleteCommand on DataList.
<div>
<asp:DataList OnDeleteCommand="Delete_Command" ID="gvImages" RepeatColumns="5" RepeatDirection="Horizontal" GridLines="Horizontal"
runat="server" BorderColor="#336699" BorderStyle="Solid" ShowHeader="true">
<ItemTemplate>
<center>
<table>
<tr>
<td style="width: 90px; height: 90px">
<img id="PICID" runat="server" src='<%# Container.DataItem %>' alt='' style="height: 100px;
width: 100px;" />
<br />
<asp:Button ID="Delete" Height="22px" CommandName="Delete" Width="100px" runat="server"
Text="Delete Picture" /><br />
</td>
</tr>
</table>
</center>
</ItemTemplate>
</asp:DataList>
</div>
Then,
For example Hold FileName:
<asp:Button CommandArgument ='<%# Container.DataItem %>' />
Then,
public void Delete_Command(Object sender, DataListCommandEventArgs e)
{
//you can hold filename on Button's CommandArgument
string fileName = e.CommandArgument;
File.Delete(Server.MapPath(fileName));
FileInfo fInfo;
fInfo = new FileInfo(fileName);
fInfo.Delete();
gvImages.DataBind();
}

How to Recognize the CommandName of a LinkButton Inside an ItemTemplate af a RadListBox

I added an itemtemplate to my radlistbox and also added one label and two linkbutton(s) in it ...
my radlistbox is like below :
<telerik:RadListBox ID="RadlbOfImageGroup" runat="server" DataKeyField="ID" DataSortField="Title"
DataSourceID="sdsImagesGroup" DataTextField="Title" DataValueField="ID" Skin="BlackByMe"
EnableEmbeddedSkins="False" Width="260px" Height="365px" EmptyMessage="no rec!"
AutoPostBack="True" OnSelectedIndexChanged="RadlbOfImageGroup_SelectedIndexChanged"
CausesValidation="False">
<ItemTemplate>
<table style="width: 100%;">
<tr style="width: 100%;">
<td style="width: 64%;">
<asp:Label ID="lblTitleOfIG" runat="server" CssClass="lbl_ListBox_IG_Title" Text='<%# Eval("Title") %>'></asp:Label>
</td>
<td style="width: 18%; text-align: center;">
<asp:LinkButton ID="lbEditIG" runat="server" CausesValidation="False" CommandName="Edit"
CssClass="lb_ListBox_IG" OnClick="lbEditIG_Click">Edit</asp:LinkButton>
</td>
<td style="width: 18%; text-align: center;">
<asp:LinkButton ID="lbDeleteIG" runat="server" CausesValidation="False" CommandName="Delete"
CssClass="lb_ListBox_IG" OnClick="lbDeleteIG_Click">Delete</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:RadListBox>
My Problem is how can I check the CommandName of LinkButtons in code above when I click on them?
(We don't have access to these LinkButtons in CodeBehind)
I know we do not need CommandName for those LinkButtons / I Just want to know is it possible to read them from codebehind?
I'm not sure if this is a standard way of addressing this issue but it's what I use:
For Each item In RadlbOfImageGroup.Items
Dim editbutton As HtmlGenericControl = item.findcontrol("lbEditIG")
//Do something with editbutton.CommandName
Dim deletebutton As HtmlGenericControl = item.findcontrol("lbDeleteIG")
//Do something with deletebutton.CommandName
Next
The above example is in VB.Net but should translate fairly easily to C# if that's what you're using.
here is the code that has been introduced by telerik team :
protected void lbDeleteIG_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
if (btn.CommandName=="Delete")
{
Response.Write("Deleted");
}
}

NumericUpDownExtender buttons same height as the textbox

I'm using the NumericUpDownExtender control, but my buttons on the side of the textbox are bigger then my textbox, is there a way to make the buttons the same height as my textbox?
Edit: I found my problem. I was using the standard buttons, which can't be handled?. Now I've created custom ones, but they won't appear on each other, but next to each other.
my code:
<asp:TextBox ID="txtHerst" runat="server" Text="0" Style="text-align: center"></asp:TextBox><cc1:NumericUpDownExtender ID="extHerst" runat="server" TargetControlID="txtHerst"
Width="50" Enabled="True" Maximum="1.7976931348623157E+308" Minimum="-1.7976931348623157E+308"
RefValues="" ServiceDownMethod="" ServiceDownPath="" ServiceUpMethod="" Tag=""
TargetButtonDownID="imgBtnDown" TargetButtonUpID="imgBtnUp"></cc1:NumericUpDownExtender><asp:ImageButton ID="imgBtnUp" runat="server" ImageUrl="Images/up.jpg" /><asp:ImageButton ID="imgBtnDown" runat="server" ImageUrl="Images/down.jpg" />`
You should be able to use css to do that...
I encountered the same problem, and eventually replaced the extender to RangeValidator.
first all of you should create some Css.
<style type="text/css">
.auto-style1 {
width: 78px;
}
.auto-style2 {
height: 5px;
width: 19%;
}
</style>
So, in my case I've created a table and set the columns with the controllers and buttons that need it, works for me
<table border="1">
<tr >
<td class="auto-style1" ><asp:TextBox ID="TextBox1" runat="server" Width="100%" Text='<%# Bind("Secuencia") %>' Height="18px"></asp:TextBox></tdstyle="width=70%> </td>
<td class="auto-style2" >
<asp:ImageButton ID="up" runat="server" style="max-height:100%; max-width:100%" ImageUrl="~/Imagenes/up.gif" Width="10px" />
<asp:ImageButton ID="down" runat="server" style="max-height:100%; max-width:100%" ImageUrl="~/Imagenes/down.gif" Width="10px" />
</td>
</tr>
</table>
<ajaxToolkit:NumericUpDownExtender ID="NumericUpDownExtender1" Width="20" runat="server"
TargetButtonUpID="up" TargetButtonDownID="down" TargetControlID="TextBox1" />

Resources