Access TextBox Control In ListView Control - c#

How to access "TextBox" control inside "ListView" Control ?
For example I want to use this.AddCommentTextbox.Text property in code-behind.
aspx code:
<asp:ListView ID="PostsListView" runat="server" DataSourceID="EntityDataSourcePosts">
<ItemTemplate>
<asp:TextBox Text="active" ID="AddCommentTextbox" runat="server" TextMode="MultiLine" Height="100" Width="370"></asp:TextBox>
</ItemTemplate>
</asp:ListView>

You could try something like this:
TextBox tmpControl = (TextBox)PostsListView.FindControl("AddCommentTextbox");
then you can do something with the tmpControl.text
Hope that helps.

Related

To get values from custom control

In the below code I have user controls like textbox, dropdown, checkbox, etc. and I bind it to a datalist. Now I have refer the controls to .aspx web page now my aim is to get the values of custom controls in .aspx page. Please help me to do this. My aim is to get the values of textbox, dropdown, checkbox from usercontrols in .aspx.
GmatField.ascx
<asp:TextBox ID="txtField" runat="server" width="200Px" CssClass="style22" ></asp:TextBox>
<asp:DropDownList ID="cbField" runat="server" width="200Px" >
</asp:DropDownList>
<asp:CheckBox ID="chField" runat="server" width="200Px" />
GmatField.ascx
<%# Register TagPrefix="gmat" TagName="FieldCont" Src="~/Search/GmatField.ascx" %>
<asp:DataList ID="dlFields" runat="server" Height="100px"
Width="50px" BorderColor="Beige">
<ItemTemplate>
<gmat:FieldCont ID="gmatFieldCont" runat="server" />
</ItemTemplate>
</asp:DataList>
NewDocument.aspx
<%# Register TagPrefix="gmat" TagName="GmatFieldsControl" Src="~/Search/GmatFields.ascx" %>
<gmat:GmatFieldsControl ID="gmatFieldsContr" runat="server" />
Simple.
Create a public property that gets the value from the textbox or selectedItem dropdownlist
Then from the page that implements that user control, you can then access the property:
// usercontrol:
public string TxtField
{
get
{
return this.txtField.Text;
}
}
// from the ASPX page that implements the usercontrol:
string txtFieldValue = this.gmatFieldsContr.TxtField;

Dynamic HyperLink

I have a HyperLink along with other controls such as Label etc in a GridView. The Label's in the GridView are dynamically populated like this:
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ClientName") %>'></asp:Label>
I am now trying to do something similar with HyperLink, as in:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='http://www.company.com?clientname=<%# Bind("ClientName") %>'>Client Name</asp:HyperLink>
This doesn't give me any errors, but the link becomes this:
http://www.company.com/?clientname=<%# Bind("ClientName") %>
instead of something like this:
http://www.company.com/?clientname=oshiro
Anyone know how to get the link to work properly instead of just outputing the asp.net code?
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("ClientName", "http://www.company.com/?id={0}") %>'>Client Name</asp:HyperLink>

ASP.NET How to know if control in page if-else is visible?

Here is an example. The problem is that the Label2 has always Visible=true, regardless of the if-else result. How can i know if its visible or not?
<asp:GridView runat="server" ID="gdv">
<asp:TemplateField>
<ItemTemplate>
<%if (!IsItem)
{%>
<asp:TextBox runat="server" Text='<%# Eval("Qtde") %>'></asp:TextBox>
<%}
else
{ %>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Qtde") %>'></asp:Label>
<%} %>
</EditItemTemplate>
</asp:TemplateField>
I cannot access IsItem, since the class where i want to check the label for visibility is not the page code-behind. Also these controls are in a gridview.
The class method that checks for visibility is not called directly by the page, its a private method of a server control that extends a grid view and is supposed to work with a varierty of cases.
Thanks.
EDIT------------
I have found an alternative, decent solution. Still, i'm curious to know if it's possible to do the aforementioned. Thanks you for your time,
You are displaying that Label based on a variable called "IsItem." Why can't you use that to determine the Label's visibility?

Using <ItemTemplate> with Checkbox and Label

I want to be able to show the Label next to the manually inserted item "All Profiles", right now it only shows the checkbox at the top but I am not sure how to pass the text to the label.
Thanks
Can you try something like that?
<telerik:radcombobox id="myCombo" emptymessage="All Types" runat="server" width="200px" AppendDataBoundItems="True">
<ItemTemplate>
<div onclick="StopPropagation(event)">
<asp:CheckBox runat="server" ID="chk1" onclick="onCheckBoxClick(this)"/>
<asp:Label runat="server" ID="lblProfile" AssociatedControlID="chk1"><%# Eval("Name") %></asp:Label>
</div>
</ItemTemplate>
<Items>
<telerik:RadComboBoxItem runat="server" Name="Hello"></telerik:RadComboBoxItem>
</Items>
</telerik:radcombobox>
Try databinding again after inserting "All profiles"-item.
If that doesnt work, try something like this:
var values = myDbConnection.GetValues();
var listOfValues = values.Select(x => new ListItem(x.Name, x.Value)).ToList(); // something like that
listOfValues.Add(new ListItem("All Profiles"));
myCombo.DataSource = listOfValues;
myCombo.DataBind();
Telerik recommends to rebind added items in the DataBound event handler like the following
ddlCombobox.Items[0].DataBind()
Check the following links for similar issue on Telerik site
http://www.telerik.com/account/support-tickets/view-ticket.aspx?threadid=327434
http://www.telerik.com/help/aspnet-ajax/combobox-insert-default-item-when-databinding.html

C# Databind question

OK, So Im fairly new to Web apps, been doing Windows app for a while in VB but Im switching to C# for web apps. Ok so I have a textbox and I have a SQLDataSource called SQLDataSource1. How in the world do I bind the source to the textbox so when it loads it will populate it?
Can someone give me some examples?
Ive tried the following,
Label1.Text = SqlDataSource1.SelectParameters.ToString();
But I get something other than the value of the SQLDataSource. I get this: System.Web.UI.WebControls.SqlDataSource
Thanks
I found an answer here on Forum.ASP.Net
Here is what has been said by Samu Zhang (Microsoft Online Community Support) :
It is hard to bind textbox to sqldatasource. We usually bind data controls such as FormView and GridView to sqldatasource and put one TextBox control in their Template . And you can invoke FindControl method of FormView to retrieve the textbox.
See my sample.
protected void Button1_Click(object sender, EventArgs e)
{
TextBox box = FormView1.FindControl("TextBox1") as TextBox;
}
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="countryid" DataSourceID="SqlDataSource1">
<ItemTemplate>
countryid:
<asp:Label ID="countryidLabel" runat="server" Text='<%# Eval("countryid") %>'></asp:Label><br />
countryname:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("countryname") %>'></asp:TextBox><br />
</ItemTemplate>
</asp:FormView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
SelectCommand="SELECT * FROM [country]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="retrieve value" />
</form>
One way is have your controls in a FormView or DetailsView, then set the DataSourceID of the FormView to the SqlDataSource and in your controls you bind them like so: asp:Label id=... Text='<%# DataBinder.Eval(Container.DataItem, "MyFieldName") %>' />, then on your OnInit of the page you can call MyFormView.DataBind();

Categories

Resources