I want to store the repeater control value to database table. I have a repeater control with three lable with corresponding textbox.
<asp:Repeater ID="RepeatInformation" runat="server">
<ItemTemplate>
<tr>
<td valign="top" class="style3">
<%#DataBinder.Eval(Container,"DataItem.fldleavename")%>
</td>
<td valign="top">
<asp:TextBox ID="TextBox1" runat="server" CssClass="textbox" ></asp:TextBox>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
repeater control format:
Casual Leave : Textbox1
Medical Leave : Textbox1
Annual Leave : Textbox1
how can i store the repeater value to database. I don't have an idea for storing this value please help me ..
foreach (RepeaterItem item in RepeatInformation.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
TextBox textBox = (TextBox)item.FindControl("TextBox1");
//Now you have your textbox to do with what you like
//You can access the .Text property to find the new value that needs saving to the database
}
}
On a side note. I would consider revising the names of your controls. It'll make life a lot easier in the future if you adopt some sort of convention and use camel case.
With regards to saving this to a database - it entirely depends on how you're dealing with data access at the moment. That's another question altogether I think.
You can store the value in list object & then bind it into data table in database.
Modify your datatable and updated into database using SQLDataAdapter
enter code here
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem != null)
{
//Create object for database
// Get Data from rep controls
// using e.Item
// store it into database
}
}
Related
I currently retrieve data from a database and store it in a data list. One of those items is a bytes value that is used to display an image. The code works, however, when there is no image available, I run into an error as a result of trying to perform operations on a null value. Is there any way to to display a default image, such as that found in the imageButton below the one in question, if there is no value in the image field of the database?
<asp:DataList ID="applicationsDataList" runat="server" RepeatColumns="4" OnItemCommand="itemCommand" >
<ItemTemplate>
<table>
<tr>
<td>
<asp:ImageButton ID="userImage" CssClass="cardImage" CommandName="profile" runat="server" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("image")) %>'/>
<%--<asp:ImageButton CssClass="cardImage" CommandName="profile" runat="server" ImageUrl="/Images/blank.png"/>--%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Thanks geniuses!
You can use C#'s ternary operator to check whether the value is null, and insert a base64 string for the default image instead.
Something like this:
ImageUrl='<%# "data:image/jpg;base64," + Eval("image") != null ? Convert.ToBase64String((byte[])Eval("image")) : Convert.ToBase64String(GetDefaultImage()) %>'
That is assuming that Eval("image") is returning null? If possible, it would be ideal to move the call to Eval() outside of the expression so that you don't call it twice (once in the condition, and once in the consequence). You can then define a function like GetDefaultImage() to return a byte array with your default image.
This is what I used to solve the problem in the end :)
ImageUrl='<%# !string.IsNullOrEmpty(Eval("image").ToString()) ? "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("image")) : "/Images/blank.png" %>' />
You could as noted work on a more "complex" expression. But then again?
I often just write code in the on-data bound event.
so, with this:
<asp:ImageButton ID="userImage" CssClass="cardImage" CommandName="profile"
runat="server"/>
Then just put the above in the data bound. It somewhat of a wash out in terms of say having to write a few extra lines of code in item data bound, or having a hard to read messy expression in the markup? (I am open to flipping a coin on this).
But, item data bound then becomes this:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
| e.Item.ItemType == ListItemType.AlternatingItem)
{
ImageButton btnImg = (ImageButton)e.Item.FindControl("userImage");
DataRowView rRow = (DataRowView)e.Item.DataItem;
if (DBNull.Value.Equals(rRow["image"]))
btnImg.ImageUrl = "~/Content/ckdelete.png";
else
btnImg.ImageUrl = #"data:image/png;base64,"
+ Convert.ToBase64String((byte[])rRow["image"]);
}
}
Note how we are able to get the data bind row values. That data set ONLY persists during the data binding events - after done, it goes out of scope.
I have a Repeater which loads some data from a SQL database
<asp:Repeater ID="Repeater" runat="server" OnItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<asp:Label ID="QuestionLabel" runat="server" Text=""></asp:Label>
<asp:TextBox ID="AnswerLabel" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="AnswerSubmit" runat="server" Text="Insert"/>
In code behind, i assign to get the questions on another button click to load and bind the Repeater. In ItemDataBound i find the controls and assign the questions to the label.
How should i get the answers that the user enters and store the ID of the question with the answer they enter in a button click event?
At first, I tried this in the button click event
foreach (RepeaterItem item in Repeater.Items)
{
Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
}
but it couldnt find the QuestionLabel. Looking at the page source i believe thats due to the label having a different value (QuestionLabel_0, QuestionLabel_1 etc), so struggling to find a way to approach this?
Edit
protected void QuestionButton_Click(object sender, EventArgs e)
{
Repeater.DataSource = QuestionsByID(ID);
Repeater.DataBind();
}
You see
foreach (RepeaterItem item in Repeater.Items)
{
if (item.ItemType == ListItemType.Item
|| item.ItemType == ListItemType.AlternatingItem)
{
Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
}
}
I have an ASP:DataList. Inside the DataList, I have the code below which displays the Name and Checkbox for each row.
What I would like to do is:
Store the Name in a hidden field.
Loop through all the checkboxes, find the ones that are checked and INSERT the value into the database.
If possible, please provide some sample code.
<td style="width: 600px"><%#Eval("Name></td>
<td style="width: 20px">
<asp:CheckBox ID="chkName" Text='<%# Eval("Name") %>' runat="server" />
</td>
what you are doing is fine.
Just in your postback, loop though the check box and check
foreach (Checkbox cb in YOurcheckboxlist)
{
if (cb.Checked) {
// get the name and insert
}
}
Had this situation before, this is what I did,
in the form_Sumbit event,
foreach (Control ctl in form1.Controls)
{
if (ctl is CheckBox)
{
//check for checked or not and store the value into an array or a List.
}
}
Not a perfect solution, let's see if someone can come up with a better idea.
There's this repeater...
<asp:Repeater ID="myRepeater" OnItemCommand="rpt1_ItemCommand" runat="server" OnItemDataBound="rpt1_OnItemDataBound">
<HeaderTemplate>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tr class="lgrey">
<td>Default</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible="True" CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
<asp:Label ID="label1" Text="Yes" runat="server" Visible="False"></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
What I want is that when user clicks on any of the "lnk1" link button in the lsit that repeater renders,
the link should be replaced with the label "label1"..ie when the user clicks on "Make Default" link, it should be replaced
with "Yes" label
Now when I click 2 link buttons, both get their label "Yes" displayed where as I want only one link button to display Yes
ie the one which has been clciked and rest of the items should display "Make Default" link button only.
ie Only ONE item should be displaying "Yes" label...now how do I iterate through the repeater items to set only ONE item
as default and not multiple ??
You can iterate the repeater items collection,
protected void myRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
int selectedIndex = e.Item.ItemIndex;
foreach(RepeaterItem item in myRepeater.Items)
{
((LinkButton)item.FindControl("lnk1")).Visible = (item.ItemIndex != selectedIndex);
((Label)item.FindControl("label1")).Visible = (item.ItemIndex == selectedIndex);
}
}
The pros of this option are: 1. no second hit on the database.
Or I would put my logic in the ItemDataBound event instead, store the clicked link button index in a member variable and call DataBind in the command event handler.
private int selectedIndex = -1;
//...
protected void myRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
selectedIndex = e.Item.ItemIndex;
myRepeater.DataSource = MyGetDataMethod();
myRepeater.DataBind();
}
In the ItemDataBound handler compare the current index with the stored index and if they match show the label.
protected void myRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if(e.Item.ItemIndex == selectedIndex)
{
((LinkButton)e.Item.FindControl("lnk1")).Visible = false;
((Label)e.Item.FindControl("label1")).Visible = true;
}
}
}
The cons of this second option are: 1. A second hit on the database. 2. If the user clicks say row two, and some other user inserts a new address record, row 2 may now be something different when you re-bind. Also if you're not using an order by that could change between database calls and your stored selectedIndex could be invalidated thatway too.
So in conclusion I'd go with option one now I've thought it all the way through.
How can i get current MainNavigationMenu hyprelink in code behind and check if is current menu clicked then i will change him default CSS.
I try with this code but is always null
HyperLink mainNavigationMenu = siteMapAsBulletedList.FindControl("MainNavigationMenu") as HyperLink;
full repeater code:
<asp:Repeater runat="server" ID="siteMapAsBulletedList" DataSourceID="smdsMenu">
<HeaderTemplate>
<li><asp:HyperLink ID="MainNavigationMenu" runat="server" NavigateUrl='<%#SiteMap.RootNode.Url%>'
Text='<%#SiteMap.RootNode.Title%>'></asp:HyperLink></li>
</HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("Url")%>' Text='<%#Eval("Title")%>'></asp:HyperLink></li>
</ItemTemplate>
</asp:Repeater>
Are you verifying which type of repeater item you are looking at ?
Attach an ItemDataBound to your repeater and do something like this :
private void rptPanier_ItemDataBound(Object sender , RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
var myItem = (Hyperlink)e.Item.FindControl("YourControlName");
}
}
Then you will have a reference on it and you can do whatever you want !
Beware tho, the ItemData item, which can be found in the repeateritemeventargs, is always null when the repeater is creating the header.
Hope this helps !
The repeater has a collection of Items. Each Item is a RepeaterItem, which has an ItemType property. For header items, this value will be "ListItemType.Header". Therefore, you want to perform .FindControl() upon that particular repeater item, not the entire Repeater itself.