Unusable elements inside the (list view ) asp.net - c#

Why can't I use the (input) I put in my (list view) in the C # code field?
<asp:ListView ID="ListV" runat="server">
<ItemTemplate runat="server">
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<tr class="text-center">
<td class="product-remove"></td>
<td class="image-prod">
<div class="">
<asp:Image ID="Image1" CssClass=" img" ImageUrl='<%# "../img/" + Eval("picture")
%> ' runat="server" />
</div>
</td>
<td class="product-name"><%# Eval("namebook") %> </td>
<td class="price"><%# Eval("Price") %> </td>
<td class="quantity"><%# Eval("titel") %></td>
<td class="col-2">
<asp:Button ID="delete" CssClass="btn btn-outline-danger" CommandArgument='<%#
Eval("id")%>' OnClick="delete_Click" runat="server" Text="حذف کالا" />
</td>
<td>
<input id="quantity2" runat="server" type="number" value="1" min="1" max="20" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>

You want a server control there. Additionally, this control only exists as part of an item template, meaning there isn't just one control named quantity2... there could be many, depending on the data source. Therefore referencing it just by name is not good enough. And speaking of data sources, depending on how you handle this the databinding may not have taken place yet at the time the Page_Load code runs, meaning the input control doesn't exist at all.
So the real question is: what do you want to do with this input?

CodeBehind="~/ (Page Name) .aspx.cs"

Related

New controls not recognized in code behind

I added a couple of fields manually in my ASPX page which is actually holding a FormView. By manually, I mean by directly typing code in the Markup editor.
Now when I'm trying to access those controls in the code behind, nothing comes up in intellisense.
I deleted the designer.cs file and right clicked the ASPX page and chose Convert to web application, still no go.
The designer file's got the FormView control defined alright, but nothing about its child controls.
Do I really need to use FindControl to get this working ? Tell me if you need some code posted because at the moment maybe I'm just a bit confused about ASP.NET is actually working (or not at this moment).
Here's the markup code:
<%# Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="WebForm_PatientForm.aspx.cs" Inherits="WebAppWalkthrough.WebForm_PatientForm" Title="Employee form" %>
<%# MasterType VirtualPath="~/Site.Master" %>
<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<asp:FormView ID="PatientForm" runat="server" DefaultMode="Edit">
<EditItemTemplate>
<table>
<tr>
<td class="FormViewHeader">
Patient Name:
</td>
<td>
<asp:TextBox ID="PatientName" ReadOnly="true" runat="server" Text='<%# Bind("FullName") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="FormViewHeader">
Patient ID:
</td>
<td>
<asp:TextBox ID="PatientID" ReadOnly="true" runat="server" Text='<%# Bind("patientid") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="FormViewHeader">
Eligibility
</td>
<td>
<asp:DropDownList runat="server" ID="Eligibility" SelectedValue='<%# Bind("service_executive_eligibility") %>' >
<asp:ListItem Value="" Text="N/A" />
<asp:ListItem Value="True" Text="Yes" />
<asp:ListItem Value="False" Text="No" />
</asp:DropDownList>
</td>
</tr>
</table>
<asp:LinkButton ID="LinkButton2" runat="server" OnCommand="UpdatePatientInfo" Text="UpdateInfo" CommandArgument='<%# Bind("patientid") %>'>Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" OnCommand="CancelEditPatient" CommandName="CancelUpdate" Text="CancelInfo">Cancel</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<table>
<tr>
<td class="FormViewHeader">
Employee First Name:
</td>
<td>
<asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="FormViewHeader">
Employee Last Name:
</td>
<td>
<asp:TextBox ID="LastName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="FormViewHeader">
Email address:
</td>
<td>
<asp:TextBox ID="EmailAddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="FormViewHeader">
Work phone number:
</td>
<td>
<asp:TextBox ID="PhoneNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="FormViewHeader">
Eligibility
</td>
<td>
<asp:DropDownList runat="server" ID="Eligibility">
<asp:ListItem Value="True" Text="Yes" />
<asp:ListItem Value="False" Text="No" />
</asp:DropDownList>
</td>
</tr>
</table>
<asp:LinkButton ID="LinkButton5" runat="server" OnCommand="AddEmployee" Text="Add Employee">Add</asp:LinkButton>
</InsertItemTemplate>
</asp:FormView>
</asp:Content>
Even if I add the controls manually in the designer file, I get a null reference error in the code behind at run time (even if intelisense picks up FirstName the object is null at runtime).
EDIT: I will try installing VS2010 SP1 and this hotfix I found and post the results.
EDIT 2: So I installed VS2010 SP1 which didn't solve the issue. I could not install the hotfix as my system is x64.
So I ditched VS2010 and tried with VS2013, which still doesn't pickup the markup controls.
Even Page.FindControl("ControlName") returns null. Something is very rotten with that webpage or VS2013...
Thanks.
I encountered a problem that seems similar with Telerik UI components for WebForms before. I wanted to access items that were defined inside of a template in a grid, but unfortunately the only way to do that was to dig into child elements of that grid. That inner elements were not present in the designer.cs file as well.
I am not sure about that, but it can be a common behavior of nested elements in WebForms.

Nested repeaters - How to access item on 3rd level without runat server tag?

Hi i have a 3 tiered nested repeater and i need to access the values in the repeater condRptr below on postback, i tried everything but dont seem to be able to get anywhere. I had the table originally within the repeater with runat server in the header template but this threw an error.
Anyone got any ideas??
<asp:Repeater ID="deptRepeater" runat="server" OnItemDataBound="bindDepts">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div id="itemContainer">
<h3 class="fdAccordionHeader"><%# Eval("desc") %><span class="fdAccordionHeaderIcon"></span></h3><asp:HiddenField ID="deptid" runat="server" Value='<%# Eval("dept_id") %>' />
<div class="fdAccordionContent" style="display:none">
<asp:Repeater ID="prodRepeater" runat="server" OnItemCommand="itemToCart">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div>
<h4 class="fdProductHeader"><span class="fdProductHeaderTitle"><%# Eval("desc") %></span><span class="fdProductFeatured"></span><span class="fdProductHeaderPrice">€<%# Eval("price") %></span><span class="fdClearBoth"></span></h4>
<div class="fdProductContent" style="display:none">
<img class="fdProductImage" src="http://wingit.ie/script/app/web/img/frontend/noimg.png" alt="">
<div class="fdProductContentRight">
<div class="fdProductDesc">A helping of our delicious wings, served with fries and a drink of your choice. Perfect if you're particularly hungry!</div>
<input type="hidden" name="product_id" value="2">
<!---->
<asp:LinkButton ID="btnOrder" runat="server" CssClass="fdButton fdButtonOrder" CommandName="addToCart" CommandArgument='<%# Eval("product") %>' ></asp:LinkButton>
<table id="condimentsContainer" cellpadding="0" cellspacing="0" runat="server">
<asp:Repeater ID="condRptr" runat="server">
<ItemTemplate>
<tr><td>
<table id='condiments' cellpadding="0" cellspacing="0" class="fdProductExtraTbl" runat="server">
<tbody>
<tr>
<td><%# Eval("pDesc") %></td>
<td class="fdProductExtraTblPrice"><asp:Label ID="priceLvl" runat="server">€<%# Eval("pPrice") %> x</asp:Label>
<input id="modifier" class="modifier" type="text" name="extra_id[3]" value="0" runat="server"/>
<input id="hdProd" type="hidden" value='<%# Eval("pCode") %>' runat="server"/>
</td>
<td class="fdProductExtraTblIco"></td>
<td class="fdProductExtraTblIco"></td>
</tr>
</tbody>
</table>
</td></tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
<span class="fdClearLeft"></span>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
Ok so as no-one had any ideas ;) i eventually figured out a nifty work around for this, because i couldn't have the runat server in the header template, i basically put it in the
element like so
<tr ID="dataRow" runat="server"> rest of data </tr>
Then in the C# code on the server side i was able to use the RepeatEreventArgs and cast the as a HtmlTableRow so that i could iterate through the data - simples :)

ASP.NET Repeater Control Checkbox issue

Overview: Clicking on text of first CheckBox in a repeater control clicks the last one. I know why this is happening; association of label tag with element id chkMarkedForDeletion and when that label is clicked it selects the last CheckBox, as all the CheckBoxes have same ID.. damn you repeater control!! I am wondering if there is any way to avoid this? Easy way would be to create individual labels and associate correctly to each CheckBox but that defeats the supposed default behaviour of a CheckBox.
Repeater code:
<asp:Repeater ID="childNodesDataRepeater" runat="server">
<ItemTemplate>
<table style="width: 100%;" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="width: 200px;">
<asp:CheckBox ID="chkMarkedForDeletion" runat="server" EnableViewState="true" Text="Remove"
Checked='<%# DataBinder.Eval(Container.DataItem, "IsMarkedForDeletion") %>' />
</td>
<td>
</td>
<td style="width: 200px;">
<asp:CheckBox ID="chkHighImpactCause" runat="server" EnableViewState="true" Text="High Impact Cause"
Checked='<%# DataBinder.Eval(Container.DataItem, "IsHighPriority") %>' />
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="header002" runat="server" Text="What caused this problem?" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:TextBox ID="txtProblemCausedBy" runat="server" EnableViewState="true" Width="100%"
Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' /><br />
</td>
</tr>
<tr>
<td colspan="3" style="height: 5px;">
<hr />
<asp:HiddenField ID="nodeIdentifier" runat="server" EnableViewState="true" Value='<%# DataBinder.Eval(Container.DataItem, "AnalysisID") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Generated HTML Code for CheckBox:
<INPUT id=chkMarkedForDeletion type=checkbox name=TabContainer$tabProblemResolution$frmProblemResolution1$childNodesDataRepeater$ctl00$chkMarkedForDeletion>
<LABEL for=chkMarkedForDeletion>Remove</LABEL>
...
...
<INPUT id=chkMarkedForDeletion type=checkbox name=TabContainer$tabProblemResolution$frmProblemResolution1$childNodesDataRepeater$ctl02$chkMarkedForDeletion>
<LABEL for=chkMarkedForDeletion>Remove</LABEL>
Issue:
Thanks,
Abhi
Make sure the ClientIDMode on the repeater is not set to "Static".
This setting is inheritable from the parent, which goes all the way up to web.config. So if you have to set the ClientIDMode explicitly on the repeater, it means that some parent overrides the default (Predictable) to "Static".

Read textbox from ASP.net page that repeated n times

I have an asp.net web page that's has a number of controls that are repeated according to number of records retrieved from the database, and I wanna read the values of those text boxes after users enter values in them, so please if anyone can help me on this issue.
I have an example of my work
<% System.Data.SqlClient.SqlDataReader myDReader = myDatabaseConnector.getDataFromDBAsSQLDataReader("SELECT * from students);
while(myDReader.Read())
{ %>
<asp:TextBox ID="txtCourseInfo" Test="" EnableViewState="false" CssClass="dataEntrySearchDataText" ReadOnly="true" runat="server"></asp:TextBox>
<% } %>
This a sample of what I have mean from my question in which I want to retrieve the value of txtCourseInfo in my C# code behind
You could use a Repeater to do this:
<asp:repeater id="rptCourses" runat="server" DataSourceID="dsCourseInfo"
<ItemTemplate>
<asp:TextBox ID="txtCourseInfo" Text='<%#Eval("StudentName")#%>' EnableViewState="false" CssClass="dataEntrySearchDataText" ReadOnly="true" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
<asp:sqldatasource id="dsCourseInfo"
ConnectionString="<%$ ConnectionStrings:YourConnectionString %>"
Select Command="Select * from Students">
</asp:sqldatasource>
That's all you need. from code behind you can iterate through the controls on the repeater and grab the information.
Notice the Eval expression on the text field. I used "StudentName" as an example, but you should use one of the column names returned by the select statement
Additional comments:
Don't do select * from ... It's a bad practice in many respects. Always specify the columns you need, even if you need all.
I realize that you disable viewstate on the textbox. I don't know the reason for doing this but be aware that the info won't be persisted on subsequent postbacks unless you enable it on the control.
UPDATE
Yes, you can have tables inside repeaters. Look at the example below:
<ItemTemplate>
<table runat="server" style="color: White; background-color: #3A4F63;" visible="false"
id="headerTable">
<tr>
<td colspan="3" align="center">
<asp:Label ID="headerTitle" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td style="width: 200px; background-color: #3A4F63; color: White;">
Image
</td>
<td style="width: 200px;">
Studen Name
</td>
<td style="width: 200px;">
Birth Date
</td>
</tr>
</table>
<table>
<tr>
<td style="width: 200px;">
<asp:Image ID="img" runat="server" ImageUrl='<%#Eval("ImageUrl") %>'></asp:Image>
</td>
<td style="width: 200px;">
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblBirthDate" runat="server" Text='<%#Eval("BirthDate") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>

Button not firing onClick below Panel controls in IE

I have a button which is not working when placed below two panels. If I move it above the panels, it works.
It works either way in Firefox. It does not work in IE 8
The button runs this code
protected void Button2_Click(object sender, EventArgs e)
{
panelForm.Enabled = true; //input panel
panelOutput.Visible = false; //output panel
Button1.Visible = true; //input panel button
}
I have some workarounds, but was hoping to find the cause of the issue.
edit: here is the markup of the second panel and button. I've tried moving the button outside of the panel and get the same result.
<asp:Content ID="MainContent" Runat="Server" ContentPlaceHolderID="MainContentPlaceHolder">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="domainUserID" runat="server" Visible="false"></asp:TextBox>
<!-- gray bar and title -->
<table style="width:100%; border-style:none;">
<tr>
<td class="com_headline">
SQL Emergency Request [Home]
</td>
</tr>
<tr class="com_app_instructions">
<td>
<p>Words here</p>
</td>
</tr>
</table>
<!-- end title and gray bar -->
<asp:Panel ID="panelForm" runat="server" Visible="True" CssClass="myform">
<form method="post" action="Default.aspx" id="form">
<h1>Request Form</h1>
<p>Complete this form to be issued a login</p>
<table cellpadding="5px">
<tr>
<td>
IR Number
<br />
<span class="small">Obtain your IR number from
SMART</span>
</td>
<td>
<asp:TextBox ID="txtIR" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtIR" CssClass="errorMsg"
ErrorMessage="Please Enter Your IR Number">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Server
<br />
<span class="small">MSSQL5 is supported for now</span>
</td>
<td>
<asp:DropDownList ID="ddServer" runat="server" AutoPostBack="True"
Enabled="False" onselectedindexchanged="ddServer_SelectedIndexChanged">
<asp:ListItem>DEVMSSQL05</asp:ListItem>
<asp:ListItem Selected="True">MSSQL05</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="ddServer" CssClass="errorMsg"
ErrorMessage="Please Choose A Server">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Database
<br />
<span class="small">You have the role of 'Analyst' in these databases</span>
</td>
<td>
<asp:DropDownList ID="ddDatabase" runat="server" AppendDataBoundItems="true"
AutoPostBack="false" DataSourceID="DatabaseDropDownObjectDataSource"
DataTextField="DatabaseName" DataValueField="DatabaseName" Width="150">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="Button1" runat="server" CssClass="com_btn_flat"
onclick="Button1_Click" Text="Submit" />
</td>
<td>
<div id="loader">loading...</div>
</td>
</tr>
</table>
</form>
</asp:Panel>
<asp:Panel ID="PanelError" runat="server" Visible="false" CssClass="errorPanel">
<h1><asp:Label ID="txtErrorMsg" runat="server">error text</asp:Label></h1>
</asp:Panel>
<br />
<asp:Panel ID="panelOutput" runat="server" Visible="false" CssClass="panelOutput">
<h1>
<asp:Literal ID="Title" runat="server" Text=""/>
</h1>
<p>
<asp:Literal ID="Warning" runat="server" Text=""/>
</p>
<p>
<asp:Literal ID="LoginLifeHours" runat="server" Text=""/>
</p>
<p>
<span class="important">
<asp:Literal ID="Login" runat="server" Text="" />
</span>
</p>
<p>
<span class="important">
<asp:Literal ID="PWD" runat="server" Text="" />
</span>
</p>
<br />
<p>
<asp:Button ID="Button2" runat="server" Text="Request Another Login"
onclick="Button2_Click" CssClass="com_btn_flat" />
</p>
</asp:Panel>
This is the button that is not responding in IE
<p>
<asp:Button ID="Button2" runat="server" Text="Request Another Login"
onclick="Button2_Click" CssClass="com_btn_flat" />
</p>
The problem is that you are using a <form> tag within your content page. The master page already includes a <form> tag and IE appears to be balking at the form within a form. When I removed the <form> tag from your aspx, the button handler ran under IE8.
The sample code is Button2_Click, but your button markup outside of the panels has test_button_Click as the event handler? There are three buttons, so which one are you asking about, I assume the last?
I noticed that you have a tag inside your first panel (panelForm). Also, I don't see a a tag with a runat="server" attribute (although, it could be in a master page). And it doesn't look like your button is inside a form (unless the master page has a form).
The problem is that you can only have one form on a page in Asp.NET WebForms. If you don't have any <form runat="server"> tags on your page, then you buttons will not fire any events on the code-behind.

Categories

Resources