I'm experiencing some trouble accessing the value of an asp element from code behind.
Basically, In my .ascx file I have a repeater and a data source. The repeater puts data from the data source (which is a database) into an HTML table like so:
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table id="VersionsTable">
</HeaderTemplate>
<ItemTemplate>
<thead>
<tr>
<th id="id">ID</th>
<th id="date">Date</th>
<th id="name">Name</th>
<th id="message">Message</th>
<th id="delete">Delete</th>
</tr>
</thead>
<tbody>
<tr>
<td> <asp:TextBox ID="idfield" runat="server" value='<%# Eval("LeaveAMessageID") %>' /></td>
<td id="idfield1"><%# Eval("LeaveAMessageID") %></td>
<td id="datefield"><%# Eval("FormInserted") %></td>
<td id="namefield"><%# Eval("TextBoxControl") %></td>
<td id="messagefield"><%# Eval("TextAreaControl") %></td>
<td id="deletebutton"><asp:Button id="Button1" Text="Delete Message" onclick="Button1_Click"
runat="server"> </asp:Button></td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Now, what i need to be able to do is to delete a table row from the database based on the value in the textbox with the id "idfield".
So when the button in the last column is clicked, the record will be deleted from the database.
The repeater and the table works as expected, they produce the output that i want. However, i can't seem to access the asp:textbox element from the code behind file.
My Button1_Click method looks like this:
public void Button1_Click(object sender, EventArgs e)
{
try
{
//TextBox f = (TextBox)this.FindControl("idfield");
//var msgID = /*Request.Form["idfield"];idfield.Value;*/Request["idfield"];
//var msgID = Request.QueryString["idfield"];
//var msgID = DataBinder.Eval(this, "idfield");
var control = (WebUserControl2)LoadControl("~/Controls/WebUserControl2.ascx");
var msgID = control.Repeater2.FindControl("idfield");
//var msgID = this.FindControl("Repeater2", 0).FindControl("idfield", 0);
//Connect to database
After this section of code follows the database connection and the execution of the SQL statement. Everything that you see outcommented in the code section above is the things I have tried to access the value of that asp:TextBox element, and with no success so far.
So my question is: How do i access the asp:TextBox element from code behind?
Not exactly what you asked but I think this is what you are looking for. Buttons have a CommandArgument property which can be used to store the id of the record you want to delete. This can be added to your button like so:
<asp:Button id="Button1" Text="Delete Message" onclick="Button1_Click"
runat="server" CommandArgument="<%# Eval("LeaveAMessageID") %>">
And can then be accessed from you click event like so:
public void Button1_Click(object sender, EventArgs e)
{
var id = ((Button)sender).CommandArgument;
Related
I have a repeater, nested with Html table rows:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table id="grid">
<thead>
<tr>
<th>SID</th>
<th>Start Date</th>
<th>End Date</th>..
<th>PDF Text</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem,"ID") %></td>
<td><%#DataBinder.Eval(Container.DataItem,"startDate") %></td>
<td><%#DataBinder.Eval(Container.DataItem,"endDate") %></td
<td>
<asp:LinkButton runat="server" ID="lbtnPDF" Text="PDF Full Text" OnClick="lbtnPDF_Click" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
When the user clicks on linkbutton, I want to get the ID of that row of repeater item and will display the PDF file which is related to that ID, in another aspx page.
I wrote on itemdatabound event :
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
LinkButton lnkBtn = (LinkButton)e.Item.FindControl("lbtnPDF");
}
How can I get column Id of repeater nested with an html table?
I don't think you are following the right approach to do it. I would choose the benefits of Repeater's ItemCommand Event to do it.
The following code might be helpful to you:
HTML/ASP.net
<asp:Repeater ID="Repeater1" DataSourceID="SqlDataSource1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Username") %>' CommandName="ViewPDF" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"ID") %>' />
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:myConnString %>' SelectCommand="SELECT * FROM [tblUserAccounts]"></asp:SqlDataSource>
As you have noticed, I have used CommandName and CommandArgument attributes in the label. I have used CommandName attribute to identify which action the user is expecting. Also the CommandArgument to pass the required data object to server.
Go to Repeater's OnItemCommand instead of ItemDataBound event. And write the code as below;
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ViewPDF")
{
Response.Redirect("~/MyPDFFiles/" + (string)e.CommandArgument);
}
}
Now run and test your Application!
Hope this helps!
I am creating several dynamic asp:Panels in asp:Lisview ItemTemplate. In panel there's a HTML table which is surrounded by link so the whole div/box is clickable. The problem is the linkbutton's OnClick event is not firing on server side. Any thoughts?
Here is the code:
<asp:Panel runat="server" ClientIDMode="Static">
<asp:LinkButton runat="server" ID="Link" OnClick="Link_Click" CausesValidation="false">
<table runat="server" id="Table" >
<thead>
<tr><th colspan="3"><%#Eval("abc")%></th></tr>
</thead>
<tbody>
<tr>
<td >
<asp:ImageButton runat="server" ImageUrl="../Images/img_4.png"/>
</td>
<td runat="server" class="data" >
<%#Eval("abc")%>
</td>
<td>
04:15
</td>
</tr>
</tbody>
</table>
</asp:LinkButton>
</asp:Panel>
LinkClick Code
protected void Link_Click(object sender, EventArgs e)
{
LinkButton link = (LinkButton)sender;
String id = link.ID;
if (id.StartsWith("T"))
Response.Redirect("Time.aspx?Id=" + id);
else
{
Response.Redirect("Chart.aspx?Id=" + id);
}
}
I figured it out. The asp:linkbutton id was being reset in the backend code and it was leading to broken link. That's why the click wouldn't work.
basically i am trying to populate a departments table and based on, say the change date, create a button for whatever departments fall within a certain date range. Problem is, the button creates but there is no data,
heres what i have so far - onPageLoad i bind the datasource which all works and displays until i try iTemDataBound
ASP.net
<asp:Repeater ID="DepartmentsList" runat="server" OnItemDataBound="DepartmentsList_ItemDataBound"/>
<HeaderTemplate>
<table id="grouptable" class="table table-bordered table-striped">
<thead>
<tr>
<th>Send</th>
<th>ID</th>
<th>Name</th>
<th>Last Modified</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<input type="checkbox" name="SendSelect[]" value="<%# Eval("Dept_ID") %>"</input></td>
<td><%# Eval("Dept_ID") %></td>
<td><%# Eval("DESC") %> </td>
<td><asp:Label ID="chg_date" runat="server" Text='<%# Eval("chg_date") %>'></asp:Label></td>
<td><a class="btn btn-info" href="<%# Eval("gURL") %>"><i class="icon-pencil icon-white"></i> Edit </a>  <asp:Button ID="bcastBtn" runat="server" Text="Send Now" CssClass="btn btn-danger" /> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
c# code
protected void DepartmentsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
{
return;
}
else
{
Repeater bcastbuttonControl = (Repeater)item.FindControl("bcastBtn");
Repeater DepartmentsList = (Repeater)item.FindControl("chg_date");
//Coding for date validation to go here - at the minute just checking based on empty or null
if (String.IsNullOrEmpty(chg_date.Text))
{
bcastBtn.Visible = false;
}
else
{
bcastBtn.Visible = true;
}
}
}
Repeater bcastbuttonControl = (Repeater)item.FindControl("bcastBtn");
Repeater DepartmentsList = (Repeater)item.FindControl("chg_date");
I think there's a problem with the above code. You should be casting the first to a button and the second to a textbox (or whatever control it is but it sure is not a repeater) your data might be disappering when you add the onitemdatabound because of an invalid cast exception here
RepeaterItem item = e.Item;
remove the above code and instead simply use:
Label chg_date = (Label)e.Item.FindControl("chg_date");
and if that doesn't work either can you post your page_Load code?
I have a ListView that creates a table. Each row in the table has two buttons - Delete and Modify. I'm firing a click event for each button but I'm not sure how to get a handle to the data in the row that the button was clicked.
aspx
<asp:ListView runat="server" ID="usersListView">
<LayoutTemplate>
<table class="table table-striped">
<thead>
<tr>
<th>User Name</th>
<th>Email</th>
<th>Role</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder"/>
</tbody>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder"/>
</tr>
</GroupTemplate>
<ItemTemplate>
<td><%# Eval("user.UserName") %></td>
<td><%# Eval("user.Email") %></td>
<td><%# Eval("roles") %></td>
<td>
<button runat="server" id="modify" class="btn btn-mini" title="Modify" onclick="modify_OnClick">
<i class="icon-edit"></i>
</button>
<button runat="server" id="delete" class="btn btn-mini" title="Delete" onclick="delete_OnClick">
<i class="icon-trash"></i>
</button>
</td>
</ItemTemplate>
</asp:ListView>
aspx.cs
public void delete_Onclick(object sender, EventArgs e) {
}
public void modify_Onclick(object sender, EventArgs e) {
}
I will try to answer the question in the title since i don't understand the question itself.
You can cast the the sender to the Button. The Button's NamingContainer is the ListViewItem. You can use this to get all your other control in that item by using item.FindControl("OtherControlID").
For example;
public void delete_Onclick(object sender, EventArgs e) {
var btn = (Button)sender;
var item = (ListViewItem)btn.NamingContainer;
// find other controls:
var btnModify = (Button)item.FindControl("modify");
}
You cannot find text that is not in a server control. But you could use a LiteralControl or Label to display the username or email.
<td><asp:Label id="Label1" runat="server"
Text='<%# Eval("user.UserName") %>' />
</td>
Now you can use FindControl as shown above to get a reference to the label in codebehind.
I know 2 solutions:
1-command name and command argument as below
2-you can use links instead of buttons and send queryString that include
the id of your record to the same page u are,and take querystring in
code behind(page load) and use it for delete sqlcommadns or redirect it to another page for
editing purposes and finally Response.Redirect() again to the same page that you are.
You should be using a GridView since the data is tabular. At any rate, you can implement the ItemCommand event. The MSDN page has an example.
You can customize CommandName and the CommandArgument properties of an Button control. The CommandArgument can be made to contain some ID or a relevant piece of information.
I have a repeater trying to get multiple data to display.
which includes a few textboxes which will show the current setting.
take note that this acts like a 'edit info' page for multiple images at once.
i also have problem displaying the images from the database.
To make it simple :
my .cs code:
DataTable ChildImageDT = myImagesBAL.GetChildImageDT(userID, childID, display);
var userList = new List<Images>();
foreach (DataRow row in ChildImageDT.Rows)
{
var child = new Images()
{
DateTaken = DateTime.Parse(row["image_taken_dt"].ToString()),
PlaceTaken = row["image_taken_loc"].ToString(),
DetailedInfo = row["image_info"].ToString()
};
userList.Add(child);
}
Repeater1.DataSource = userList;
Repeater1.DataBind();
my .aspx code
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table class="content_background">
<tr>
<td width= "10%">Date Taken:</td>
<td><asp:TextBox ID="txtName" Text="<%#Eval("DateTaken")%>" Visible="true" runat="server" Height="100px" Width="100px"></asp:TextBox></td>
</tr>
<tr>
<td width= "10%" bgcolor=aqua>Place Taken:</td>
<td bgcolor=blue ><asp:TextBox ID="txtPassword" Text="<%#Eval("PlaceTaken")%>" Visible=true runat="server" BackColor="White" Font-Size="Large" ForeColor="Fuchsia" Height=50px ></asp:TextBox></td>
</tr>
<tr>
<td width= "10%">Detailed Info:</td>
<td><asp:TextBox ID="TextBox1" Text="<%#Eval("DetailedInfo")%>" Visible=true runat="server" ></asp:TextBox></td>
</tr>
</table>
</ItemTemplate>
my output as shown:
note: that the output is in the "text: " but the whole text box doesnt appear.
You might be getting a "The server tag is not well formed." error.
Just change your Eval code to single quotes instead of a double quote e.g.
Text="<%# Eval("DateTaken") %>" // It's understood as string text
to
Text='<%# Eval("DateTaken") %>' // now understood as server side code.