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">
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 am tring to Create a Comment box having reply option for my Website
Below Is My aspx page
<div>
<asp:Repeater runat="server" ID="repAnswer">
<ItemTemplate>
<h6>Answer</h6>
<p><%# Eval("Answer") %></p>
<asp:Label runat="server" ID="lblAnsId" Text='<%# Eval("AnsId")%>' Visible="false"></asp:Label>
<a class="link" id='lnkReplyParent<%#Eval("AnsId") %>' href="javascript:void(0)" onclick="showReply(<%#Eval("AnsId") %>);return false;">Reply</a>
<a class="link" id="lnkCancle" href="javascript:void(0)" onclick="closeReply(<%#Eval("AnsId") %>);return false;">Cancle</a>
<div id='divReply<%#Eval("AnsId") %>' style="display:none;">
<asp:TextBox ID="textCommentReplyParent" CssClass="input-group" runat="server" Width="300px" TextMode="MultiLine" ></asp:TextBox>
<br />
<asp:Button ID="btnReplyParent" runat="server" Text="Reply" OnClick="btnReply_Click" /></div>
</ItemTemplate>
</asp:Repeater>
</div>
<div style="margin-top:100px">
<h5>Your Answer</h5>
<hr />
<CKEditor:CKEditorControl ID="txtAddAnswer" BasePath="Admin/ckeditor/" runat="server">
</CKEditor:CKEditorControl>
<asp:Button runat="server" ID="btnAnswer" Text="Submit Answer" OnClick="btnAnswer_Click"/>
</div>
i have used Repeater to bind my answer or comment. Inside the repeater i have given two link pne is of reply other is of cancel.when someone click on the reply a new textbox and button open which is used to give the reply
Below is my cs page
protected void btnReply_Click(object sender, EventArgs e)
{
foreach (RepeaterItem row in repAnswer.Items)
{
Label lblNewAnsIdholder = (Label)row.FindControl("lblNewAnsIdholder");
TextBox txtReplyToAnswer = (TextBox)row.FindControl("txtReplyToAnswer");
OnlineSubjects onlinesub = new OnlineSubjects()
{
reply = txtReplyToAnswer.Text.Trim(),
AnsId = Convert.ToInt32(lblNewAnsIdholder.Text.ToString())
};
onlinesub.addAnswer();
}
}
i dont know how to use textbox in repeater but after searching it through google i get something like this which i am not sure thats right or wrong.
And the line where i have created my object for my class i am getting error from there
I am tring to pass the value of textbox as a paramter
Plz help me to do this.
Thank You
You Should try,
TextBox txtReplyToAnswer = ((TextBox)row.FindControl("txtReplyToAnswer")).Text;
Hope this will work.
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 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
}
I created LinkButton that located inside of Repeater Control.
CategoryID is a variable in LinkButton Control that have to get value after Repeater Control was bound to data. But CategoryID always get zero.
I have the following ASP and C# code:
<asp:Repeater ID="rpt1" runat="server"
OnItemDataBound="rpt1_ItemDataBound"
OnItemCommand="rpt1_ItemCommand">
<ItemTemplate>
<div>
<%# Eval("Name") %>-<%# Eval("CollectionType")%>
<asp:LinkButton ID="LinkButton1" runat="server" Text="[edit item]"
PostBackUrl='AddItem.aspx?CategoryID=<%# Eval("CollectionID")%>' />
</div>
</ItemTemplate>
</asp:Repeater>
Code behind:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<GlassesCollection> gc = BL.GetDataBL.GetCollection();
rpt1.DataSource = gc;
rpt1.DataBind();
}
}
Any idea why CategoryID variable doesn't get any value and how can I fix the problem?
A server control parameter cannot contain a mixture of literal text and evaluated expressions.
The code you have will literally be posting back to AddItem.aspx?CategoryID=<%# Eval("CollectionID")%> and it will not be evaluating the code within the angle brackets.
You need to change your parameter like so
PostBackUrl='<%# "AddItem.aspx?CategoryID=" + Eval("CollectionID")%>' />