I develop user control based on repeater. It must contains predefined checkBox for each row.
Any suggestions?
Perhaps, something like this?
Default.aspx
<%# Page Language="C#" %>
<script runat="server">
protected override void CreateChildControls()
{
base.CreateChildControls();
LoadUserControl();
}
private void LoadUserControl()
{
ph.Controls.Clear();
Control c = LoadControl("~/WebUserControl.ascx");
c.ID = "foo";
ph.Controls.Add(c);
}
</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="ph" runat="server" />
</div>
<asp:Button runat="server" />
</form>
</body>
</html>
WebUserControl.ascx
<%# Control Language="C#" %>
<%# Import Namespace="System.Data" %>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!IsPostBack)
{
DataTable dt = GetRawDataTable();
dt.Rows.Add(dt.NewRow());
rpt.DataSource = dt;
rpt.DataBind();
}
}
private static DataTable GetRawDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("product", typeof(string)));
dt.Columns.Add(new DataColumn("checked", typeof(bool), "false"));
return dt;
}
protected void OnBtnAddClicked(object sender, EventArgs e)
{
DataTable dt = GetRawDataTable();
foreach (RepeaterItem ri in rpt.Items)
{
TransferFromPage(ri, dt);
}
dt.Rows.Add(dt.NewRow());
rpt.DataSource = dt;
rpt.DataBind();
}
private static void TransferFromPage(RepeaterItem ri, DataTable dt)
{
DataRow row = dt.NewRow();
row["product"] = ((TextBox)ri.FindControl("txtProduct")).Text;
row["checked"] = ((CheckBox) ri.FindControl("chk")).Checked;
dt.Rows.Add(row);
}
protected void OnRptItemCmd(object sender, RepeaterCommandEventArgs ea)
{
switch (ea.CommandName)
{
case "RemoveRow":
DataTable dt = GetRawDataTable();
foreach (RepeaterItem ri in rpt.Items)
{
if (ri.ItemIndex == ea.Item.ItemIndex) continue;
TransferFromPage(ri, dt);
}
rpt.DataSource = dt;
rpt.DataBind();
break;
}
}
</script>
<div>
<table>
<asp:Repeater ID="rpt" runat="server" OnItemCommand="OnRptItemCmd">
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="txtProduct" runat="server" Text='<%# Eval("product") %>' />
<asp:CheckBox ID="chk" runat="server" Checked='<%# Eval("checked") %>' />
<asp:Button ID="btnRemove" runat="server" Text="-" CommandName="RemoveRow" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>
<asp:Button ID="btnAdd" runat="server" Text="+" OnClick="OnBtnAddClicked" />
</td>
</tr>
</FooterTemplate>
</asp:Repeater>
</table>
</div>
you can use the Repeater's ItemTemplate, to put necessary controls. Below is an example:
<asp: Repeater>
<ItemTemplate>
<asp:CheckBox ID="cbkWhatever" runat="server" Checked='<%# Eval("CbkDataField") %>' />
All other items for Repeater ...
</ItemTemplate>
</asp:Repeater>
Related
I have such kind of a design in a project. How can I reach button click event in repeater nested datalist in asp.net?
<asp:DataList ID="dlPosts" runat="server" Width="100%" RepeatLayout="Flow"
RepeatColumns="1" OnItemCommand="dlPosts_ItemCommand"
OnItemDataBound="dlPosts_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="repImgs" runat="server">
<ItemTemplate>
<img src="<%#Eval("Picture") %>" style="height: 35px; width: 35px" alt="" align="middle" valign="top" />
<asp:LinkButton ID="lbYorum" runat="server" class="w3-btn w3-green w3-hover-orange" CommandName="MyUpdate" CommandArgument='<%#Bind("YazarID") %>'> Send</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:DataList>
You can just attach OnCommand event to LinkButton like regular button control.
<asp:LinkButton ID="lbYorum" runat="server"
class="w3-btn w3-green w3-hover-orange"
CommandName="MyUpdate"
CommandArgument='<%#Bind("YazarID") %>'
OnCommand="lbYorum_Command"> Send</asp:LinkButton>
Code Behind
Then retrieve YazarID from e.CommandArgument.
protected void lbYorum_Command(object sender, CommandEventArgs e)
{
string commandName = e.CommandName;
string yazarID = e.CommandArgument.ToString();
}
* Update *
if I add a new TextBox in repeater, how can I get the value of it?
You can use Parent.FindControl to find sibling controls.
...
<ItemTemplate>
<img src="<%#Eval("Picture") %>" style="height: 35px; width: 35px" alt="" align="middle" valign="top" />
<asp:LinkButton ID="lbYorum" runat="server"
class="w3-btn w3-green w3-hover-orange"
CommandName="MyUpdate"
CommandArgument='<%#Bind("YazarID") %>'
OnCommand="lbYorum_Command"> Send</asp:LinkButton>
<asp:TextBox ID="txtYorum" runat="server" Height="50" Width="500" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
...
protected void lbYorum_Command(object sender, CommandEventArgs e)
{
string commandName = e.CommandName;
string yazarID = e.CommandArgument.ToString();
var control = sender as Control;
var txtYorum = control.Parent.FindControl("txtYorum") as TextBox;
}
asp DropDownList inside ListView with UpdatePanel. SelectedIndexChanged Not fire when select item from dropdownlist.
Already Tryied Every Solution.
OnPageLoad()
!isPost
AutoPostBack="True"
ViewStateMode="Enabled"
EnableViewState="true"
<asp:ListView ID="productListView" runat="server" OnItemDataBound="productListView_ItemDataBound">
<ItemTemplate>
<div class="item col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="hovereffect">
<div class="overlay">
<div class="selection">
<asp:Label runat="server" CssClass="lblShowTag">BRAND:</asp:Label>
<asp:DropDownList ID="ddlBrand" runat="server" CssClass="branddrop" AutoPostBack="True" OnSelectedIndexChanged="ddlBrand_SelectedIndexChanged" ViewStateMode="Enabled" EnableViewState="true">
</asp:DropDownList>
<asp:UpdatePanel ID="updatePanel" runat="server" >
<ContentTemplate>
<asp:Label runat="server" ID="lblShowTag" text="PRICE: " CssClass="lblShowTag"/><asp:Label runat="server" ID="lblEachPrice" CssClass="lblEachPrice"/>
<div class="clearfix"></div>
<asp:Label runat="server" class="lblShowTag">QUANTITY: </asp:Label>
<asp:TextBox ID="txtQuantity" runat="server" Text="1" CssClass='txtquantity' />
<div class="clearfix"></div>
<asp:Label runat="server" ID="Label1" text="TOTAL: " CssClass="lblShowTag"/><asp:Label runat="server" ID="lblTotalPrice" CssClass="lblEachPrice" ForeColor="Green" Text="5000"/>
<div class="clearfix"></div>
<asp:Label id="lblError" runat="server" Text="Label 2" ForeColor="Red"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlBrand" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</div>
</div>
</div>
</ItemTemplate>
<EmptyDataTemplate>
<h3>Sorry...No Product Availabel</h3>
</EmptyDataTemplate>
</asp:ListView>
C# Code:
protected void Page_Load(object sender, EventArgs e)
{
//showProdutListView();
String CateId = Request.QueryString["CateId"];
if (!String.IsNullOrEmpty(CateId))
{
showProdutListView(CateId);
}
else
{
Response.Redirect("/default.aspx");
}
if (!IsPostBack)
{
if (Session["myCart"] != null)
{
DataTable dt = (DataTable)Session["myCart"];
cartQuantity = dt.Rows.Count.ToString();
}
else
{
cartQuantity = "0";
}
((ProdutcPageNested)Master).OnProductMasterPage.Text = "Cart Items: " + cartQuantity;
ShowBrand();
}
}public void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListViewItem item in productListView.Items)
{
DropDownList ddl = (DropDownList)sender;
lblEachPrice.Text = ddl.SelectedValue.ToString();
}
}
The problem here is you are binding data on page load, so whenever DDL is posting page loads new values causing not to fire OnSelectdIndedChanged event. Here is my solution hope it works.
this should be replaced
if (!String.IsNullOrEmpty(CateId))
{
showProdutListView(CateId);
}
else
{
Response.Redirect("/default.aspx");
}
if (!IsPostBack)
{
if (Session["myCart"] != null)
{
DataTable dt = (DataTable)Session["myCart"];
cartQuantity = dt.Rows.Count.ToString();
}
else
{
cartQuantity = "0";
}
((ProdutcPageNested)Master).OnProductMasterPage.Text = "Cart Items: " + cartQuantity;
ShowBrand();
}
with this
if (!IsPostBack)
{
if (!String.IsNullOrEmpty(CateId))
{
showProdutListView(CateId);
}
else
{
Response.Redirect("/default.aspx");
}
if (Session["myCart"] != null)
{
DataTable dt = (DataTable)Session["myCart"];
cartQuantity = dt.Rows.Count.ToString();
}
else
{
cartQuantity = "0";
}
((ProdutcPageNested)Master).OnProductMasterPage.Text = "Cart Items: " + cartQuantity;
ShowBrand();
}
I need to assign a value of linkbutton selected from datalist to a label.
How can i do this?
.aspx code
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
style="margin-left: 5px"
onselectedindexchanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Eval("Description") %>' Font-Underline="False" ForeColor="Black" Font-Italic="False" ToolTip="click to open"></asp:LinkButton>
<br />
</ItemTemplate>
</asp:DataList>
<br />
<asp:Label ID="item" runat="server" Text="Label"></asp:Label>
.aspx.cs
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
item.Text = DataList1.SelectedIndex.ToString();
}
this can be done like this :
<asp:Label1 ID="Label1" runat="server"
Text='<%# Bind("your datafield") %>'
Try the following:
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
var myLink = DataList1.SelectedItem.FindControl("LinkButton2") as LinkButton;
if (myLink != null)
{
item.Text = myLink.Text;
}
}
Here is the code:
<asp:Repeater runat="server" ID="repeater1" OnItemCommand="checkChange">
<ItemTemplate>
<asp:Table runat="server" Width="80%">
<asp:TableRow runat="server" HorizontalAlign="Center" Width="80%">
<asp:TableCell Width="80%" HorizontalAlign="Left">
<asp:Button ID="Button1" runat="server" Text="删除" Visible="<%#administrator %>" />
<asp:CheckBox runat="server" ID="checkBox" Text='<%#DataBinder.Eval(Container.DataItem,"IID")%>' AutoPostBack="true" OnCheckedChanged="checkChange" EnableViewState="false"/>
<asp:Label runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"invText") %>'></asp:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:Repeater>
and the function is:
protected void checkChange(object sender, EventArgs e)
{
Response.Write("it works well!");
}
but when I click the checkbox,nothing happen...Thank you ...
Because you have provided single event handler to both the CheckBox and the Repeater.
But of course because both OnItemCommand and OnCheckedChanged events has same delegate signatures i.e void target(object sender, EventArgs e) so it won't raise any kind of error.
Code working fine like this :
<asp:Repeater runat="server" ID="repeater1">
<ItemTemplate>
<asp:Table ID="Table1" runat="server" Width="80%">
<asp:TableRow ID="TableRow1" runat="server" HorizontalAlign="Center" Width="80%">
<asp:TableCell Width="80%" HorizontalAlign="Left">
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CheckBox runat="server" ID="checkBox" Text="Click me if u dare." AutoPostBack="true"
OnCheckedChanged="checkChange" EnableViewState="false" />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:Repeater>
Back end
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
for (int i = 0; i < 5; i++)
dt.Rows.Add(new object[] { "Name no.: " + i.ToString() });
repeater1.DataSource = dt;
repeater1.DataBind();
}
}
protected void checkChange(object sender, EventArgs e)
{
Response.Write("Clicked called at :" + DateTime.Now.ToString("dd-MM-yy hh:mm:ss"));
}
I have repeater with item template:
<asp:Repeater ID="queryParametersRepeater" runat="server"
DataSourceID="queryParametersObjectDataSource">
<ItemTemplate>
<tr class="itemTemplate">
<td class="labelTd" style="width: 300px;">
<asp:HiddenField runat="server" Value='<%# Eval("ParameterType") %>' />
Define <%# Eval("ParameterName") %> (type <%# Eval("ParameterType") %>)
</td>
<td class="valueTd">
<asp:TextBox runat="server" Width="300px" Text='<%# Eval("ParameterName") %>' />
<asp:CheckBox runat="server" Width="300px" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
In jQuery I change display property of textBox and chekBox. How can I get data from those controls?
I can't use FindControls() function, because I don't know id of my controls.
You can use ID's and findcontrol to get the values. Here is a working example:
<%# Page Language="C#" %>
<%# Import Namespace="System.Collections.Generic" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind the data source
RepeaterExample.DataSource = new List<KeyValuePair<int, string>>{
new KeyValuePair<int, string>(1, "Test1"),
new KeyValuePair<int, string>(2, "Test2"),
new KeyValuePair<int, string>(3, "Test3")
};
RepeaterExample.DataBind();
}
}
protected void cmdSubmit_Click(object sender, EventArgs e)
{
//read the values and output them
litResults.Text = "";
foreach (RepeaterItem i in RepeaterExample.Items)
{
TextBox txtExample = (TextBox)i.FindControl("txtExample");
if (txtExample != null)
{
litResults.Text += txtExample.Text + "<br />";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="RepeaterExample" runat="server">
<HeaderTemplate>
test</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtExample" runat="server" Text='<%#Eval("Value") %>'></asp:TextBox>
</ItemTemplate>
</asp:Repeater><br />
<asp:Button ID="cmdSubmit" runat="server" Text="Submit" OnClick="cmdSubmit_Click" />
<br />
<asp:Literal ID="litResults" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
Can you add a class to your controls?
If so you could try with $(".givenClass").