How to Find Current Row of Repeator in parent repeater? - c#

<asp:Repeater ID="rp1" runat="server">
<ItemTemplate>
<ul>
<li>
<%#Module(Convert.ToInt32(Eval("id")),Eval("university").ToString()) %>
<asp:Repeater ID="rp2" runat="server">
<ItemTemplate>
<ol>
<li id="sublist" runat="server">
<%#Eval("college") %>
</li>
</ol>
</ItemTemplate>
</asp:Repeater>
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
C# CODE
public string Module(int id,string university)
{
con = new SqlConnection(ConStr);
con.Open();
cmd = new SqlCommand("SELECT college FROM tbluniversity where id=" + id, con);
da = new SqlDataAdapter(cmd);
con.Close();
ds = new DataSet();
da.Fill(ds);
//Here I want to find the inner repeator of current row then bind this dataset to that.
how to find Repeator
return university;
}

you have to add a ItemCreated event and you can use e.Item.FindControl method to find your inner repeater using its id

Related

How can I fetch values from a same column into different textbox?

I am having difficulty in displaying the dynamic data into text-boxes. I am in a situation where I have multiple text-boxes and I want to fetch value from table where column name is 'registration_charges' and 'amount'. Now column registration_charges' contains data e.g., 'OPD_CHARGES', 'DR.Charges' and 'amount' column has data e.g.,:- 5000, 1000. I want to display amount in text box accordingly.
<asp:Label runat="server" >OPD Ch.</asp:Label>
<asp:TextBox ID="txtopd_charges" runat="server" Width="100px"
style="text-
align: right;float:right;margin-right:15px;">/-</asp:TextBox>
<asp:Label runat="server" >DR Ch.</asp:Label>
<asp:TextBox ID="txtdr_charges" runat="server" Width="100px"
style="text-
align: right;float:right;margin-right:15px;">/-</asp:TextBox>
Back-end code:
//connection code
con.Open();
SqlCommand cmd = new SqlCommand("SELECT amount FROM
Mst_Charges WHERE registration_charges IN('OPD_CHARGES',
DR.Charges')", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txtopd_charges.Text = ???
txtdr_charges.Text = ???
}
else
{
}
}
catch (Exception ex)
{
}
}
In your query you must select registration_charges along with the amount
SqlCommand cmd = new SqlCommand("SELECT amount,registration_charges FROM
Mst_Charges WHERE registration_charges IN('OPD_CHARGES',
DR.Charges')", con);
You can fetch data of your DataTable like this:
txtopd_charges.Text = dt.Rows[0]["amount"].ToString();
txtdr_charges.Text = dt.Rows[0]["registration_charges"].ToString();
UPDATED
You can bind the data with Repeater and initialized your textbox inside of the ItemTemplate which will repeat your data exist in DataTable.
Aspx Code
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:TextBox ID="txtopd_charges" Text='<%# Eval("amount") %>' runat="server"></asp:TextBox>
<asp:TextBox ID="txtdr_charges" Text='<%# Eval("registration_charges") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
and in your C#
if (dt.Rows.Count > 0)
{
Repeater1.DataSource = dt;
Repeater1.DataBind();
}

After some idle time images not load from database

My problem is when I click on the product page (in URL below). It works good but after some idle time on first click on product menu it wont load product images from database to Repeater control.
I am using Bootstrap and not using update-panel.
Please Refer Below Link.
MySite(See Product Page On First Time And Click After 5 to 10 minute of idle time)
My ASP.net code is
<asp:Repeater ID="rptrProduct" runat="server">
<ItemTemplate>
<div class="portfolio-item col-xs-12 col-sm-4 col-md-3">
<div class="recent-work-wrap hovereffect">
<img id="dtimg1" runat="server" class="img-responsive imggray" src='<%#Eval("ImagePath")%>' alt="" style="height: 185px!Important;" />
<div class="overlay1">
<div class="recent-work-inner">
<a id="aimg2" runat="server" href='<%#Eval("ImagePath")%>' rel="prettyPhoto">
<h2>
<%#Eval("ProductName")%></h2>
</a>
<p>
<a id="adtimg1" runat="server" class="preview" href='<%# string.Format("~/BMSubProduct.aspx?pro={0}", Eval("ProductId")) %>'>
<i class="fa fa-eye"></i>View Products</a>
</p>
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
I am just not getting that why on first time it won't load any images.
my C# page code is
if(!Page.IsPostBack)
{
DataTable dt = DBMaster.getQueryDataTableSchema(query);
if (dt.Rows.Count > 0)
{
string path = GlobalFunction.getPath(1);
NoProducts.Visible = false;
for (int i = 0; i < dt.Rows.Count; i++)
dt.Rows[i]["ImagePath"] = path + Convert.ToString(dt.Rows[i] ["ImagePath"]);
rptrProduct.DataSource = dt;
rptrProduct.DataBind();
}
else
{
NoProducts.Visible = true;
rptrProduct.DataSource = null;
rptrProduct.DataBind();
}
}
public static DataTable getQueryDataTableSchema(string query)
{
try
{
using (IDbConnection con = sq.Open())
{
IDbCommand cmd = con.CreateCommand();
cmd.CommandText = query;
IDbDataAdapter adp = sq.GetAdapter(cmd);
DataSet ds = new DataSet();
adp.FillSchema(ds, SchemaType.Source);
adp.Fill(ds);
con.Close();
return ds.Tables[0];
}
}
catch (Exception ex)
{
return new DataTable();
}
}
and masterpage code in page_load is
if (!Page.IsPostBack)
{
SQLString.Config = myconnectionString;
}
Here NoProduct => No Products Are Available Right Now.
I have tried many things like below:
EnableViewState="true" And EnableViewState="false" both for repeater and in page also.
Change connection string in web.config like
name="SQLConStr" connectionString="Server=localhost;Database=databasename; Uid = username; Pwd = password; Pooling = false; Connection Timeout = 30"
Here tried with and without connection timeout and pooling.
Note: Using MySQL database and using interface to open database connection.
Thanks in advance.
EnableViewState="true"
and try to use
<asp:Image and <asp:HyperLink controls
instead of
img and a tags with runat="server".

Order gallery images as they are uploaded

I am using facebox on my asp.net web site for my image gallery. when i am uploading images to the gallery, they are saved on my disc and url data is stored in my sql database. After the uploading, my gallery displays thumbnails from the images but not in the order as they are uploaded. I want to display the last uploaded image as first in the gallery (order by last uploaded), but i don't know what i should add in the code.
This is the code:
<body style="background-color:black">
<script type="text/javascript" charset="utf-8">
$(function () {
$('[rel^="FaceBox"]').FaceBox();
});
</script>
<form id="form1" runat="server">
<div class="Znamenitosti" id="Znamenitosti">
<asp:DataList ID="dlImages" runat="server" RepeatColumns="7" CellPadding="3" >
<ItemTemplate>
<div class="boxButton">
<ul class="Gallery" >
<li><a id="A1" href='<%# Eval("ime","~/Sliki/Ohrid/Znamenitosti/{0}") %>' title='<%# "Од "+ Eval("userid")+ ", на " + Eval("datum")+ ", " + Eval("opis")%>' rel="FaceBox[gallery1]" runat="server" >
<asp:Image ID="Image1" ImageUrl='<%# Bind("imethumb", "~/Sliki/Ohrid/Znamenitosti/thumb/{0}") %>' runat="server" Width="140" Height="140" AlternateText='<%# Bind("imeslika") %>' />
</a></li></ul></div>
</ItemTemplate>
</asp:DataList>
</div>
Cs code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}
protected void BindDataList()
{
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["makbazaConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
if (Request.QueryString["ID"] == "Znamenitosti")
{
//Query to get ImagesName and Description from database
SqlCommand command = new SqlCommand("SELECT ime, imethumb, imeslika, kategorija, datum, opis, slikapateka, thumbpateka, userid FROM Ohrid WHERE kategorija='Znamenitosti' AND grad='Ohrid' ", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dlImages.DataSource = dt;
dlImages.DataBind();
}
.
.
.
.
con.Close();
}
Is datum your date field?
If so just modify your sqlcommand:
SELECT ime, imethumb, imeslika, kategorija, datum, opis, slikapateka, thumbpateka, userid FROM Ohrid WHERE kategorija='Znamenitosti' AND grad='Ohrid' ORDER BY DESC datum
Cheers,
Bartek

How to use repeater inside the repeater

It's possible to use repeater inside the another repeater? How?
my problem is, the back-end can't see the repeater inside the repeater.
FRONT:
<div id="blogright">
<ul class="accordion">
<asp:Repeater ID="parentRep" runat="server">
<ItemTemplate>
<li class="2010">
<%# Eval("DYear") %><span><%# Eval("PCount") %></span>
<ul class="sub-menu">
<asp:Repeater ID="childRep" runat="server">
<ItemTemplate>
<li><em><%# Eval("DDay") %></em><%# Eval("DMonth") %><span><%# Eval("ICount") %></span></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
BACK:
//This is for parentRep
daString = "SELECT datepart(YEAR,BLG_DATE) as DYear,COUNT(BLG_DATE) as PCount FROM [BLG] INNER JOIN [ACC] ON [BLG].ACC_ID=[ACC].ACC_ID WHERE [BLG].ACC_ID='"+userID+"' GROUP BY BLG_DATE";
SqlDataAdapter da2 = new SqlDataAdapter(daString, conn);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
parentRep.DataSource = dt2;
parentRep.DataBind();
//This is for childRep
dt.Clear();
daString = "SELECT DATEPART(DAY,BLG_DATE) as DDay,datename(month,BLG_DATE) as DMonth,DATEPART(YEAR,BLG_DATE) as DYear FROM [BLG] INNER JOIN [ACC] ON [BLG].ACC_ID=[ACC].ACC_ID WHERE [BLG].ACC_ID='"+userID+"' and BLG_DATE like '%"+urlId+"%'";
SqlDataAdapter da3 = new SqlDataAdapter(daString, conn);
DataTable dt3 = new DataTable();
da2.Fill(dt3);
//can't see the childRep here.
Sorry for my English.
You can get a reference to the child Repeater and bind data to it in the ItemDataBound event this way:
protected void parentRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater childRepeater = (Repeater)e.Item.FindControl("childRep");
childRepeater.ItemDataBound += new RepeaterItemEventHandler(childRepeater_ItemDataBound);
childRepeater.ItemCommand += new RepeaterCommandEventHandler(childRepeater_ItemCommand);
childRepeater.DataSource = dt3; //dt3 is the DataTable from your code sample
childRepeater.DataBind();
}
}
Additionally, there are some very thorough answers in this thread: Repeater in Repeater

On link click from menu BindDataList

I have this code:
$(document).ready(function () {
$('#Priroda').hide();
$('#priroda_').click(function () {
$('#Znamenitosti').hide();
$('#Priroda').show();
});
$('#znamenitosti_').click(function () {
$('#Priroda').hide();
$('#Znamenitosti').show();
});
});
</script>
<div class="meni">
<nav>
<ul>
<li class="active">Info</li>
<li>Znamenitosti</li>
<li>Priroda</li>
<li>Noken Zivot</li>
</ul>
</nav>
</div>
where I have few categories in the menu. When i click on specific category, i want to show the (div) tag for that specific category within the same .aspx page, and the other (div) tags for the other categories should be hidden.
This is the (div) tag for the category "Priroda":
<div class="Priroda" id="Priroda">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="7" CellPadding="3">
<ItemTemplate>
<div class="boxButton">
<ul class="Gallery">
<li><a id="A1" href='<%# Eval("ime","~/Sliki/Ohrid/Priroda/{0}") %>' title='<%# "Од "+ Eval("userid")+ ", на " + Eval("datum")+ ", " + Eval("opis")%>' rel="FaceBox[gallery1]" runat="server" >
<asp:Image ID="Image1" ImageUrl='<%# Bind("ime", "~/Sliki/Ohrid/Priroda/{0}") %>' runat="server" Width="140" Height="140" AlternateText='<%# Bind("imeslika") %>' />
</a></li></ul></div>
</ItemTemplate>
</asp:DataList>
</div>
For binding the DataList i used this code in .cs file:
protected void BindDataList1()
{
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["makbazaConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
//Query to get ImagesName and Description from database
SqlCommand command = new SqlCommand("SELECT ime, imeslika, kategorija, datum, opis, slikapateka, userid FROM Ohrid WHERE kategorija='Priroda'", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dlImages.DataSource = dt;
dlImages.DataBind();
con.Close();
}
Now i don't know how to call BindDataList1(); to show me data when i click on that category. I have different BindDataLists for each category. Can you tell me how to call specific BindDataList for the category that is selected from the menu? For example when i click on link Priroda show me the (div) tag Priroda and BindDalaList1();, when i click on link Znamenitosti show me the (div) tag Znamenitosti and BindDalaList2();
I think a simple way to do this would be to run your databind functions for all categories at once, for example in the PreRender event of "DataList1", so all of the data is on the page, and each bit is just hidden or shown through your jQuery.
Alternatively, could you use instead of tags, and have an OnClick property set to call your DataBind1 method.

Categories