New to asp.net web forms and I'm having a problem figuring out how to control variables within the loops in my HTML. For Example
<%for (int j = 0; j < Model.Route.Length; j ) { %>
<div >
<hr />
<h3 >Route
<%=j %>
</h3>
<div >
<asp:Label ID="Source1_Route1_ID_Label" runat="server" CssClass="col-lg-5">ID</asp:Label>
<asp:TextBox ID="Source1_Route1_ID_Input" runat="server" CssClass="col-lg-6"></asp:TextBox>
</div>
<div >
<asp:Label ID="Source1_Route1_Input_Label" runat="server" CssClass="col-lg-5">Input</asp:Label>
<asp:TextBox ID="Source1_Route1_Input_Input" runat="server" CssClass="col-lg-6"></asp:TextBox>
</div>
<div >
<asp:Label ID="Source1_Route1_Output_Label" runat="server" CssClass="col-lg-5">Output</asp:Label>
<asp:TextBox ID="Source1_Route1_Output_Input" runat="server" CssClass="col-lg-6"></asp:TextBox>
</div>
</div>
<% } %>
<!-- Start Wrapping Routes Rows -->
The above code looks at an array and makes repeats this section for each instance in the array. The problem is that I need control over each of the instances, but right now if in the code behind section I call Source1_Route1_ID.Input.Text = "TEST"
It will change all created instances to "TEST". I wanted to create them like <asp:TextBox ID="Source1_Route<%=j%>_ID_Input>
but it's throwing an error saying I can't use <%= %>
In my ID. Is there an easier way to solve this problem?
CodePudding user response:
In most cases, you don't need or want a loop.
And in most cases, you don't need to inject/put the code in the markup
So, you want to "repeat" somthing over and over?
And then how do you wire up click events etc.? (huge pain point).
So, in webforms land, unlike PHP etc. that inter-mingle code,and use code to spit out markup?
In the VAST majority of cases, with webforms you don't take that design patter approach. More amazing, in 99% of cases you do not need to.
So, a bit of a "gear" shift is required with webforms, and the result is less effort, less code, and no looping in most cases is required.
So, you want to "repeat" something?
Then use a "repeater" (great name, don't you think???).
So,
Say this markup:
<asp:Repeater ID="repHotels" runat="server" >
<ItemTemplate>
<asp:Label ID="txtHotel" runat="server" Width="200px"
Text='<%# Eval("HotelName") %>' Font-Size="X-Large" >
</asp:Label>
<asp:TextBox ID="txtDescription" runat="server"
Text='<%# Eval("Description") %>'
TextMode="MultiLine" Rows="4" Columns="40" Style="margin-left:20px" >
</asp:TextBox>
<asp:Button ID="cmdView" runat="server" Text="View Hotel" CssClass="btn"
style="margin-left:35px;margin-top:25px"
OnClick="cmdView_Click" CommandArgument=<%# Eval("ID") %>
/>
<div style="border-top: 1px solid black;margin-top:8px;margin-bottom:8px"></div>
</ItemTemplate>
</asp:Repeater>
So, you put the markup you want to repeat inside of the item template.
thus, code to fill is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData();
}
void LoadData()
{
SqlCommand cmdSQL =
new SqlCommand("SELECT * FROM tblHotelsA ORDER BY HotelName");
repHotels.DataSource = MyRstP(cmdSQL);
repHotels.DataBind();
}
public DataTable MyRstP(SqlCommand cmdSQL)
{
DataTable rstData = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (cmdSQL)
{
cmdSQL.Connection = conn;
conn.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
And now we get/see this:
but, above is NOT really the bonus part.
The button click MOST certainly is the Rossetta stone, since now we have a simple button, but it tied to the row of data.
So this button click code:
protected void cmdView_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
RepeaterItem gRow = (RepeaterItem)btn.NamingContainer;
Debug.Print("row index click = " gRow.ItemIndex);
Debug.Print("PK passed by button = " btn.CommandArgument);
Label txtHotel = (Label)gRow.FindControl("txtHotel");
Debug.Print("Value of Hotel = " txtHotel.Text);
}
Output:
row index click = 1
PK passed by button = 5
Value of Hotel = Inns of Banff
It stands to reason, not "only" would one want to repeat data, but ALSO tie/have/enjoy/use some button click for each row of data.
Note also, do you see any looping??? (nope!!)
Do you see messy code inermixed with the markup? (nope!!!).
So, webforms requires a bit of a different kind of thinking, and design patter approach. Once you get the hang of webforms? You will have sorrowful symphany for how most other framework's function, since as above shows, you don't have to write much code to repeat data, and bonus points is you can simply drop in a button into that repeating data, and get the row information with great ease.