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>
Related
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"
I display the list of table names when a particular database is selected from the drop down list. Number of tables in my database is more than 100. I don't want to display it as a single column and scroll the page. I want to display it as a table. Also each table name is a link to view its corresponding table details. can anyone help me with the technique to achieve it ?
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<td colspan="5">
Tables
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<%--<tr>--%>
<td><asp:Label ID="lbl_TableName" runat="server" Text='<%#Eval("table_name")%>' /></td>
<%-- </tr>--%>
</ItemTemplate>
<%-- <SeparatorTemplate>
<td colspan="4"> name</td>
</SeparatorTemplate>--%>
<%-- <AlternatingItemTemplate>
<td> break</td>
</AlternatingItemTemplate>--%>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
so basically I have a query that populates a table in an asp page like so:
<div class="span10">
<table class="table table-bordered table-striped with-check" runat="server" id="noncompulsarymodules">
<tbody>
<tr>
<td><asp:Label ID="Label3" runat="server" Text= '<%# Eval("ModuleId") %>' style="display:none;" /> </td>
<td><asp:Label ID="Label1" runat="server" Text= '<%# Eval("noncompcode") %>' /> </td>
<td><asp:Label ID="Label2" runat="server" Text= '<%# Eval("noncomptitle") %>' /> </td>
<td><asp:Label ID="Label4" runat="server" Text= '<%# Eval("moduleunits") %>' /> </td>
<td class="center" style="overflow:hidden;"> <asp:Label ID="ModuleDescription" runat="server" Text='<%# Eval("noncompdesc") %>' /></td>
<td><input type="checkbox" /></td>
</tr>
</tbody>
</table>
</div>
basically what I want to do is cycle through the table and take the rows that have been checked and put them into a query.
Any tips would be greatly appreciated!
Have a look at using an ASP.Net Gridview control with a checkbox / templatefield column.
You will then be able to iterate through the GridViewRows and get the checked columns.
I ran into the error: Cannot get inner content of ExportDiv because the contents are not literal I did a search and got this very useful resource!
I know I used server controls in my aspx page. The page fetches data dynamically from the database. How can I export this data to excel given that there're server controls in my html page.
Here're some of the site.aspx code to the page
<form id="form1" runat="server">
<div runat="server" id="ExportDiv">
<asp:Panel ID="ResultsPanel" runat="server">
<table cellpadding="0" class="style1">
<tr>
<td class="style2">
<asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4"
</asp:DetailsView>
</td>
<td>
Poor</td>
<td>
Good</td>
<td class="style3">
Very Good</td>
<td>
Total Responses</td>
<td>
Average Score</td>
</tr>
<tr>
<td class="style2" colspan="6" bgcolor="#CCCCCC">
1. How would you rate the food served to you?</td>
</tr>
<tr>
<td class="style2">
a.) Overall Quality Taste and Flavour.<br />
b.) Variety of Food.</td>
<td>
<asp:Label ID="lblResult0" runat="server" Text="Label"></asp:Label><br/>
<asp:Label ID="lblResult3" runat="server" Text="Label"></asp:Label>
</td>
<td>
<asp:Label ID="lblResult1" runat="server" Text="Label"></asp:Label><br/>
<asp:Label ID="lblResult4" runat="server" Text="Label"></asp:Label>
</td>
<td class="style3">
<asp:Label ID="lblResult2" runat="server" Text="Label"></asp:Label><br/>
<asp:Label ID="lblResult5" runat="server" Text="Label"></asp:Label>
</td>
<td>
<asp:Label ID="aveNum0" runat="server" Text="Label"></asp:Label>
<br/>
<asp:Label ID="aveNum1" runat="server" Text="Label"></asp:Label>
<br/>
</td>
<td>
<asp:Label ID="aveScore0" runat="server" Text="Label" style="font-weight: 700"></asp:Label>
<br />
<asp:Label ID="aveScore1" runat="server" Text="Label" style="font-weight: 700"></asp:Label>
<br/>
</td>
</tr>
</table>
</asp:Panel>
</div>
<asp:GridView ID="gvSurveyResult" runat="server">
</asp:GridView>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
And here's also the site.aspx.cs code
Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
Response.Write(ExportDiv.InnerHtml);
Response.End();
All answers would be welcomed!
It's been a while since I've done Server Controls, etc., but will this do the trick?
http://harouny.com/2012/10/15/render-asp-net-controls-user-controls-to-html-by-code/
I would suggest you to create a function that will convert your data into comma separated values and then render the same as CSV instead of excel format. Both CSV and excel format files would open the same way in excel.
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".