Binding Data to server controls in asp:repeater - data-binding

I would like to have a repeater control bound to method and display the result as a list of linkbuttons, but I can't get by head around it. This is what I've tried:
In the asp page I have:
<asp:Repeater ID="resultCountRepeater" runat="server" Visible="false" >
<ItemTemplate>
<asp:LinkButton ID="userResultCount" runat="server" OnClick="userResultCount_Click" Text="<%# DataBinder.Eval(Container.DataItem,"Text") %>" >
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
And in the code behind:
List<ListItem> resultCountList = new List<ListItem>();
foreach (ISearchEngine oneEng in engines)
{
ListItem item = new ListItem();
item.Text = oneEng.ObectName();
item.Value = Convert.ToString(oneEng.PageCount(searchWords, townId));
resultCountList.Add(item);
}
resultCountRepeater.DataSource = resultCountList;
resultCountRepeater.DataBind();
Unfortunately this is giving me a compile error: The server tag is not well formed.
Any ideas what it wrong?
Thanks

Use single quotes to dynamically set properties.
<asp:LinkButton ID="userResultCount" runat="server" OnClick="userResultCount_Click" Text="<%# DataBinder.Eval(Container.DataItem,"Text") %>" ></asp:LinkButton>
Should be
<asp:LinkButton ID="userResultCount" runat="server" OnClick="userResultCount_Click" Text='<%# DataBinder.Eval(Container.DataItem,"Text") %>' ></asp:LinkButton>

Related

Getting value on the literal1.text in asp.net

I would like to ask if how can I get the value inside the <span> tag in literal1.text.
This is my code.
Literal1.Text = Literal1.Text & "<div class='chip' style='padding:8px;display:inline-block;border-radius: 25px;background:white;color:black;'><span>" & TextBox3.Text & "<i class='close fa fa-times'></i></span></div> "
I using it a tagbox in my project.
Here's a screenshot
Or if you have any suggestions on how can I create a tagbox in asp.net webforms that I can easily retrieve the data and save it in the database. I would really need and will appreciate the help!
Thanks in advance!
I would use a formview around the Textbox to insert the Tags into the database and then retrieve it and insert with a Repeater.
Add your query into the SqlDataSource and edit the Bind to the name of your column name.
Please note I haven't tested it but to my knowledge, it should work.
<asp:FormView runat="server" id="FormView1" DataSourceID="SqlDataSource1">
<InsertItemTemplate>
<asp:TextBox runat="server" id="TextBox1" Text='<%# Bind("TagName") %>'></asp:TextBox>
<asp:Button runat="server" id="InsertButton" CommandArgument="Insert"></asp:Button>
</InsertItemTemplate>
</asp:FormView>
<asp:Repeater runat="server" id="Repeater1" DataSourceID="SqlDataSource1">
<ItemTemplate>
<div class='chip' style='padding:8px;display:inline-block;border-radius: 25px;background:white;color:black;'>
<asp:Label Text='<%# Bind("TagName") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<!-- Add here your database querys for insert and select -->
<asp:SqlDataSource runat="server" id="SqlDataSource1"></asp:SqlDataSource>

ASP.Net GridView DropDownList not posting back with updated data

I have a GridView that contains DropDownLists. They function as expected except during page postback. When the user clicks the update button on the page, I have a sub that loops through the grid rows, performs business ops and saves the data.
The problem is that during postback, the DropDownLists' selected properties do not represent that changes to selections made by the user. The selected item shows 'Dirty = True' in the break point.
Here is a subset of the code I use for reference:
<asp:GridView ID="materialGridView" runat="server"
AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="MaterialTypeName" HeaderText="Material Type" />
<asp:TemplateField>
<HeaderTemplate>
Quantity
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="quantityTextBox" runat="server" Width ="50" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Material
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="materialDropDownList" runat="server" Width="200">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Color
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="colorTextBox" runat="server" Width="100" BackColor="BlanchedAlmond" ReadOnly="true" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
RBK
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton id="rbkRadioButton" runat="server" Checked="true" />
<asp:TextBox ID="rbkPriceTextBox" runat="server" Width="50" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Wimsatt
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="wimsattPriceTextBox" runat="server" Width="50"></asp:TextBox>
<asp:RadioButton id="wimsattRadioButton" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For Each row As GridViewRow In materialGridView.Rows
With row
materialDropDownList = DirectCast(.FindControl("materialDropDownList"), DropDownList)
quantityTextBox = DirectCast(.FindControl("quantityTextBox"), TextBox)
rbkPriceTextBox = DirectCast(.FindControl("rbkPriceTextBox"), TextBox)
wimsattPriceTextBox = DirectCast(.FindControl("wimsattPriceTextBox"), TextBox)
colorTextBox = DirectCast(.FindControl("colorTextBox"), TextBox)
rbkRadioButton = (DirectCast(.FindControl("rbkRadioButton"), RadioButton))
'compare current selecton in drop down and update if nessisary
For Each materialTableRow As DataRow In materialTable.Rows
'the item we are on is the item selected ANDALSO it is not yet assigned to the quote, so it's a new selection, update pricing.
Dim materialTableRowMaterialID As String = bizClass.dbCStr(materialTableRow.Item("MaterialID"))
If materialTableRowMaterialID = materialDropDownList.SelectedValue Then
If IsDBNull(materialTableRow.Item("QuoteID")) Then
rbkPriceTextBox.Text = bizClass.dbCStr(materialTableRow.Item("RBK"))
wimsattPriceTextBox.Text = bizClass.dbCStr(materialTableRow.Item("Wimsatt"))
End If
End If
Next materialTableRow
If rbkRadioButton.Checked = True Then
materialCostSumDecimal += bizClass.strToDec(quantityTextBox.Text) * bizClass.strToDec(rbkPriceTextBox.Text)
chosenSupplierString = "RBK"
Else
materialCostSumDecimal += bizClass.strToDec(quantityTextBox.Text) * bizClass.strToDec(wimsattPriceTextBox.Text)
chosenSupplierString = "Wimsatt"
End If
End With 'row
First of all make sure you are not binding the gridview on postback before reading dropdown values, because databind causes all controls to lose their postback value.
Second gridview is only able to postback the value of the only row that is being edited, so what you need to do is to move the dropdown (and all other controls) to the EditTemplate and set the row mode to Edit so the value will get posted back to server.
But if you want to be able to change all the dropdowns in all rows you might need to use Repeater control instead of GridView.

Hyperlink not displayed properly

I want to display data on a page dynamically from database.
I have added a news box and I am displaying events list in repeater from a database. Hyperlink and Marquee is also used. But hyperlink is not displayed properly.
The code is given below:
<asp:HyperLink ID = "HyperLink1" runat = "server" NavigateUrl = "/events/events.aspx?id=<%#Eval('event_id') %>">
<asp:Label ID = "Label1" runat = "server" text = '<%# Eval("event_title") %>' ></asp:Label></asp:HyperLink><br/>
ASP.NET HyperLink should be declared like this:
<asp:HyperLink
ID="HyperLink1"
runat="server"
NavigateUrl="/events/events.aspx?id=<%#Eval('event_id') %>"
Text='<%# Eval("event_title") %>' />
change your code as
<asp:HyperLink
ID="HyperLink1" runat=server
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "event_id", "/events/events.aspx?id={0}") %>'>
<%# DataBinder.Eval(Container.DataItem, "event_title") %>'
</asp:HyperLink>

binding dataitem to columns in gridview

<%# ((DataRowView)Container.DataItem)["SomeProperty"] %>
<%# DataBinder.Eval(Container.DataItem, "SomeProperty")%>
From Google i figured out these can be used to bind the columns in GridView to ArrayList. But what is "some property" ?
For example i have a ArrayList in .aspx.cs as
static ArrayList componentSelectionArray = new ArrayList();
so can i just write in grid view to bind a arraylist to grid view columns as:
<asp:GridView ID= "GridView1" runat="server" AutoGenerateColumns="true">
<Columns>
<asp:TemplateField HeaderText="ComponentName">
<ItemTemplate>
<asp:Label ID="" text= "<%# DataBinder.Eval(Container.DataItem, "componentSelectionArray")%>" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
Please help me
Thank you in anticipation
To bind to an ArrayList you just have to get the the underlying DataItem.
Assuming your ArrayList is storing a string you just have to do:
<asp:Label ID="" Text="<%# GetDataItem().ToString() %>"></asp:Label>
GetDataItem(): Gets the data item at the top of the data-binding context stack.
More info on MSDN.

asp.net add templatefield items for many dropdownlist to detailsview control

How can I add the following templatefield programmatically for each of the dropdownlist control inside the details view?
<asp:TemplateField HeaderText="Your Gender">
<EditItemTemplate>
<asp:DropDownList ID="ddlGender" runat="server"
DataSourceid="ddlDAGender"
DataTextField="Gender" DataValueField="GenderID"
SelectedValue='<%#Bind("GenderID") %>'
>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate >
<asp:Label Runat="server" Text='<%# Bind("Gender") %>' ID="lblGender"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Maybe something like:
Dim tc As New System.Web.UI.WebControls.TemplateField
tc.HeaderTemplate = Page.LoadTemplate("Controls/DatagridCheckboxColumnHeader.ascx")
tc.ItemTemplate = Page.LoadTemplate("controls/DatagridCheckboxColumn.ascx")
tc.HeaderText = "Select"
grdView.Columns.Insert(0, tc)

Resources