I have a table in DB:
NOTICE(NUM,TITLE,CONTENT)
I use Repeater Control in ASP to show all the notices in DB, like:
+----------+------------+|
|title1 (readmore)|
|
|title2 (readmore)|
|
|title3 (readmore)|
......
+------------------------+
All I want is: I read a "title" then I clicked on (readmore), the new page will be opened ( show detail's notice) with the "content" of that notice. How can I assign the num of notice without display it to define the notice in next page?
I just assign the title to property Text of a Label ID="TITLE" because I want to show the title of each notice.
All information I want to show in the this page is: title and the readmore( link to the next page). So that I don't know how to assign the num
My asp page: notice.asp
<asp:Repeater ID="RepDetails" runat="server" >
<HeaderTemplate>
<table style=" width:565px" cellpadding="0" class="borber">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Title" runat="server" Text='<%#Eval("TITLE") %>' />
</td>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" > (readmord)</asp:HyperLink>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
My C# code:notice.asp.cs
private void BindRepeaterData()
{
string sql = "select num,title from NOTICE";
DataTable ds = l.EXECUTEQUERYSQL(sql);
RepDetails.DataSource = ds;
RepDetails.DataBind();
}
And the next page: detailnotice.asp.cs
private void GetNotice()
{
string sql = "select * from NOTICE where num=" ;// num= the num of notice I choose in notice.asp page.
}
How can I assign a num in Label without display it? What property of Label Control or I should use a other Control ?
Hope you understand what I say. If you don't, please ask?
Basically the same as Sain but using the NavigateURL
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("NUM","~/detailpage.aspx?id={0}") %>' > (readmord)</asp:HyperLink>
hi you can ues anchor tag in place of hyper link button. you can pass num in the query string to the detail page.
<a href='detailpage.aspx?id=<%#Eval("NUM") %>'> (readmord)</a>
On details page you can get query string value and fetch details from database.
int myKey = 0;
if (!string.IsNullOrEmpty(Request.QueryString["id"]))
{
myKey = int.Parse(Request.QueryString["id"]);
// Use myKey to retrieve record from database
}
Related
I have a repeater, one column of which contains a textbox on which is attached a JQuery datepicker along with an update button. So a user uses the datepicker to change the date, clicks update and it writes to the database.The original date and the record id are stored in hidden fields.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:HiddenField ID="hidThisID" Value='<%# Eval("orderID") %>' runat="server" />
<asp:HiddenField ID="hidPrevDueDate" Value='<%# Eval("Duebeforedate", "{0:dd/MM/yyyy}") %>' runat="server" />
<input type="text" class="form-control fc-datepicker duedateinput" style="width: 150px" value='<%# Eval("Duebeforedate", "{0:dd/MM/yyyy}") %>' runat="server" id="fccd" readonly />
<asp:LinkButton ID="btnDateUpdate" OnClick="btnDateUpdate_Click" CssClass="btn btn-primary btn-sm" runat="server" ToolTip="Approved"> Update </asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
which calls this function
protected void btnDateUpdate_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
HtmlInputText htmlDueDate = (HtmlInputText)item.FindControl("fccd");
HiddenField hidID = (HiddenField)item.FindControl("hidThisID");
HiddenField hidOldDate = (HiddenField)item.FindControl("hidPrevDueDate");
DateTime prevDueDate = Convert.ToDateTime(hidOldDate.Value.ToString());
DateTime newDueDate = Convert.ToDateTime(htmlDueDate.Value.ToString());
string ID = hidID.Value;
if (prevDueDate != newDueDate)
{
string query = "update [tblOrders] set Duebeforedate=CONVERT(datetime,'" + newDueDate.ToString() + "', 103) where [orderID] = '" + ID + "'";
//database stuff here
}
}
}
However, the newDueDate variable still holds the original due date. Consequently the old and new dates match and so the database doesn't get updated. How do I get it to store the new value?
For two-way data binding you need to use the Bind keyword instead of Eval
Fixed it, it wasn't the btnUpdate function at all, I'd forgotten this check -
if (!IsPostBack)
{
//bind the repeater
}
I've got a button inside a repeater control, and I need to send some identifying information from the button to the onClick event so I know which ID has been clicked. My asp.net code (in part) looks like this:
<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound">
<ItemTemplate>
<div class="bevelBox"><h3><b><%#Eval("AlbumName") %> Photo Gallery</b></h3></div>
<asp:Repeater ID="PhotoRepeater" runat="server" onitemcommand="PhotoRepeater_ItemCommand" OnItemDataBound="PhotoRepeater_DataBinding">
<HeaderTemplate>
<div class="bevelBox">
<Table>
<tr>
<td style="width:160px"><h3>Concert Photo</h3></td>
<td style="width:535px"></td>
<td><asp:Button ID="btnAddPhoto" runat="server" OnClick="btnAddPhoto_Click" Text="Add Yours" CommandArgument='<%#Eval("AlbumID") %>' UseSubmitBehavior="false" /></td>
</tr>
</Table>
</div>
Someone told me I could use the CommandArgument command to get this data to the code-behind.
On the C# side, I have this:
protected void btnAddPhoto_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string ggg = btn.CommandArgument.ToString();
mdlAddPhoto.Enabled = true;
mdlAddPhoto.Show();
}
I put a break point on the "string gggg" line, just to see what is being passed back. There's nothing indicating the <%#Eval("AlbumID") %> is being passed back.
Can anyone tell me how I can get this ID data passed back to the C# side?
I have a listview control which is populated with data from a database. I have used the label to display that data. Now I need to use the value of that label in some other place. I am trying to access the text value of the label control but it says
Object reference not set to an instance of an object.
But I do have value in the label.
<asp:ListView ID="msg_list" runat="server">
<ItemTemplate>
<table>
<tr class="myitem">
<td>
<asp:Label ID="reg_id_reply" runat="server" Text="helo" Visible="false" />
<asp:Label role="menuitem" ID="msg_lbl" runat="server" text='<%#Eval("msg")%>' /><i style=" color:Gray; " > from
<asp:Label ID="tme" runat="server" Text='<%#Eval("name")%>' />
<i> on </i>
<asp:Label ID="tmelbl" runat="server" Text='<%#Eval("tme")%>'/>
<a id="msg-reply" class="btn button" data-toggle="modal" data-target="#msg-rply" style="cursor:pointer;" ><i class="glyphicon glyphicon-share-alt white"> </i></a> </td>
<hr style=" margin-top:1px; margin-bottom:1px; " />
</tr>
</table>
<%--<hr style=" margin-top:1px; margin-bottom:1px; " />--%>
</ItemTemplate>
</asp:ListView>
This is how i tried to access the text of the label.Note that the code below is inside a button click event onClick of buttom.
Label mylbl = (Label)msg_list.FindControl("reg_id_reply");
string rid = mylbl.Text;
According to the description for Control.FindControl Method (String):
This method will find a control only if the control is directly contained by the specified container.
The direct container that your label is contained within is the ItemTemplate. Therefore you may need to build a recursive search for your label if you are starting with the listview. You could try the following code that is suggested from this MSDN page:
private Control FindControlRecursive(Control rootControl, string controlID)
{
if (rootControl.ID == controlID) return rootControl;
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn = FindControlRecursive(controlToSearch, controlID);
if (controlToReturn != null) return controlToReturn;
}
return null;
}
However also note that the listview may change the id values of components in the item template. If you know the item index you want then you can use the method suggested in this post:
var theLabel = this.ChatListView.Items[<item_index>].FindControl("reg_id_reply") as Label;
I've an asp repeater which has some fields inside an ItemTemplate. Each item in the repeater has an "add to cart" asp:ImageButton and an invisible asp:Label as well. The code looks like this:
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="addToCart">
<HeaderTemplate>
<table id="displayTable">
</HeaderTemplate>
<ItemTemplate>
<td>
<!-- fields like name, description etc in the repeater are present; i've omitted to show them here-->
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />
</td>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
When a particular ImageButton in the repeater is clicked, I'm trying to display "added to cart" as the text of its corresponding Label, and make the clicked ImageButton Visible=false. I've tried using the OnItemCommand function for the ASP:Repeater. The method is "addToCart":
<>
void addToCart(Object Sender, RepeaterCommandEventArgs e)
{
Cart cart = new Cart();
cart.instrument_id = //id of product from repeater based on user click
String userName = Membership.GetUser().ToString();
cart.user_name = userName;
cart.quantity = 1;
var thisLbl = (Label)e.Item.FindControl("addedToCartLabel");
var thisImg = (ImageButton)e.Item.FindControl("addToCartImg");
try
{
database.Carts.InsertOnSubmit(cart);
database.SubmitChanges();
thisImg.Visible = false;
thisLbl.Text = "Added to Cart!";
thisLbl.Visible = true;
}
catch (Exception ex)
{
thisImg.Visible = false;
thisLbl.Text = "Processing failed;please try again later";
thisLbl.Visible = true; ;
}
}
The aspx page is populated properly. However, when I click on any of the ImageButtons in the repeater, I get the following error:
Server Error in '/mysite' Application.
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page.
For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.
If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback
or callback data for validation.
Can someone help me with this?
May I suggest doing this with client-side javascript rather than a server-side call?
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />
becomes
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" onclick="javascript:function() { this.this.style.display='none'; document.getElementById(this.parentNode.firstChild.id).style.display='block'; }" ImageUrl="hi.jpg" Width="75px" Height="50px" />
I'm currently developing a guestbook system, and I need admins to remove specific logs. The problem is I'm getting an error when trying to delete it.
The error is: Index was outside the range. It may not be negative and must be less than the crowd size.
Parameter name: index
My code: asp.net
<asp:DataList ID="guestBookDataList" runat="server" ondeletecommand="guestBookDataList_DeleteCommand">
<ItemTemplate>
<div id="guest-books">
<span class="guestBook-Id"> # <%#DataBinder.Eval(Container.DataItem, "id") %></span><span class="guestBook-Name">Name: <%#DataBinder.Eval(Container.DataItem, "Name") %> </span>
<span class="guestBook-Date"> Date: <%#DataBinder.Eval(Container.DataItem, "Date") %> </span> <br />
<span class="guestBook-Text"> <%#DataBinder.Eval(Container.DataItem, "text") %> </span>
<asp:LinkButton ID="LinkButtonDelete" CssClass="gastbok-remove" runat="server" CommandName="Delete">Delete<%#DataBinder.Eval(Container.DataItem, "id") %></asp:LinkButton>
</div>
</ItemTemplate>
</asp:DataList>
My linkbutton is the delete button for each item in the datalist.
And my C# code that's giving me the error:
protected void guestBookDataList_DeleteCommand(
object source, DataListCommandEventArgs e)
{
int id = Convert.ToInt32(guestBookDataList.DataKeys[e.Item.ItemIndex]);
db.deleteGeustBookLog(id); //calls the sql command
bindList();
}
Basiclly I just need to retrive the guestbooklogs ID from the databse so I can delete it.
Been trying a lot of different methods and googling about this but I can't get it to work. So now I'm asking you. Thanks for your time.
Add a CommandArgument to the LinkButton with the ID of it. Then in the delete code, look for e.CommandArgument, and pass that value instead.
<asp:LinkButton id="LinkButton2"
Text="Delete"
CommandName="Delete"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "id") %>'
OnCommand="DeleteGuestBookEntry"
Runat="server"/>
You could also give it a commandName like "DeleteGuestBookEntry" or something.
void DeleteGuestBookEntry(Object sender, CommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
db.deleteGeustBookLog(id); //calls the sql command
bindList();
}
I'd always check to make sure that your ID is not null, and is numeric, etc, and add some error handling, but that's the down and dirty way to do it. :)
You should set "datakeynames" property
<asp:DataList ID="guestBookDataList" runat="server" ondeletecommand="guestBookDataList_DeleteCommand" datakeynames="id">