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;
Related
I've searched in google and bing and seen all posts and even in stackoverflows search but couldn't find the solution.
To be sure about the error I've created a new project it has 1 page which has:
<asp:ListView runat="server" ID="ListView" DataSourceID="SqlDataSource" OnSelectedIndexChanged="ListView_OnSelectedIndexChanged" OnSelectedIndexChanging="ListView_OnSelectedIndexChanging">
<ItemTemplate>
<asp:Label runat="server" ID="Label" Text='<%#Eval("UserName") %>' ></asp:Label><br/>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource" SelectCommand="Select * FROM Users" ConnectionString="<%$ConnectionStrings:DefaultConnection %>"></asp:SqlDataSource>
Output:
Because I've 2 users with name A and B.
Now the problem is that events OnSelectedIndexChanged and OnSelectedIndexChanging are not firing when I click on A or B. How to get the selected index where user is clicking?
Complete Project if anyone wants to look, actually there is no need to: https://app.box.com/s/bhs682q6fqai784kao0zv1tcxgxecr4s
ListView.SelectedIndexChanging Event occurs when an item's Select button is clicked, but before the ListView control handles the select operation.
(A Select button is a button control whose CommandName property is set to "Select".)
Change your code to
<asp:ListView runat="server" ID="ListView" DataSourceID="SqlDataSource" OnSelectedIndexChanged="ListView_OnSelectedIndexChanged" OnSelectedIndexChanging="ListView_OnSelectedIndexChanging" AutoPostBack="true">
<ItemTemplate>
<asp:Label runat="server" ID="Label" Text='<%#Eval("UserName") %>' ></asp:Label>
<asp:LinkButton runat="server" ID="ButtonSelect" CommandName="Select" Text="SelectButton"/><br/>
</ItemTemplate>
</asp:ListView>
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.
I am trying to access an asp Image tag in my code behind so that I can display a default image if there is no other image present. The problem that I am having is that I am getting an "Object reference not set to an instance of an object." error message.
The aspx page
<%# Control Language="C#" AutoEventWireup="true" CodeFile="MemberProfileList.ascx.cs"
Inherits="UserControls_MemberProfileList" %>
<%--<asp:GridView ID="GridView1" runat="server">
</asp:GridView>--%>
<asp:DataList ID="profilelist" runat="server" RepeatColumns="2" OnDataBinding="profilelist_DataBound" >
<ItemTemplate>
<asp:Image ID="ProfileImage" runat="server" ImageUrl='<%# System.String.Format("/Images/{0}", DataBinder.Eval(Container.DataItem, "ProfilePictureThumb"))%>' />
<asp:Hyperlink runat="server" NavigateUrl='<%#Link.ToSpecificMemberProfile(Eval("Username").ToString()) %>' Text='<%#HttpUtility.HtmlDecode(Eval("Username").ToString()) %>' />
</ItemTemplate>
</asp:DataList>
The method in the code behind
profilelist.DataSource = myProfileList;
profilelist.DataBind();
Image ProfileImage = profilelist.FindControl("ProfileImage") as Image;
string profilepic = ProfileImage.ImageUrl.ToString();
if (profilepic == null)
{
ProfileImage.Visible = false;
}
Any help is appreciated
The Image will be inside an Item, not on the whole List.
Implement your logic inside the ItemDataBound event.
Check this out: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx
Regards
//if you're trying to hide it base on weather the ProfilePictureThumb is Null by putting:**
<asp:Image ID="ProfileImage" Visible='<%# String.IsNullOrEmpty(Eval("ProfilePictureThumb").ToString())? false:true %>' runat="server" ImageUrl='<%# System.String.Format("/Images/{0}", DataBinder.Eval(Container.DataItem, "ProfilePictureThumb"))%>' />
//if you want to make it display a default image based on weather or not ProfilePicture is null:
ImageUrl='<%# String.IsNullOrEmpty(Eval("ProfilePictureThumb").ToString())? System.String.Format("/Images/{0}", "defaultimage.jpg"):System.String.Format("/Images/{0}", DataBinder.Eval(Container.DataItem, "ProfilePictureThumb"))%>'
//that way you don't need code behind.
I want to be able to get the current bound object in the ItemTemplate of a ListView control.
Here's an example of what I want to do:
<asp:ListView ID="UserList" runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
//How can I get the current bound object in here?
</ItemTemplate>
</asp:ListView>
You can access it via the DataItem:
<%# DataBinder.Eval(Container.DataItem, "myPropertyName")%>'
If you wanted a textbox for example:
<asp:Label ID="MyProp" runat="server" Text='<%#Eval("myPropertyName") %>' />
If you just want the full object:
<%# (MyType)Container.DataItem %>
I have one page which is placed in the Master Page.
In the master page I have 1 dropdown and one GridView, dropdown is display the category, based on the dropdown list selection it will display the list of videos in the Grid.
In the content page I have the video player, in the page load it will play the video by default.
But when I choose the drop down list which is available in the master page, the page is refreshing, SO the video is start play from the first.
The content page should not refresh, So the video will continuously play.
How can I stop the page refresh in the content page?
All are in master page.
`<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<asp:DropDownList ID="drp_Channel" Width="220px" CssClass="ddl"
AutoPostBack="true" runat="server"
onselectedindexchanged="drp_Channel_SelectedIndexChanged">
<asp:ListItem>-- Select Channels --</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="up2" runat="server" UpdateMode="Conditional">
<asp:GridView ID="grd_Video" runat="server" AutoGenerateColumns="False" OnRowCommand="LinkName"
GridLines="None" ShowHeader="False" Width="100%" EmptyDataText="No Videos Found" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="img_Video" runat="server" BorderColor="#666699" CssClass="imgbox"
ImageUrl='<%#(string)FormatImageUrl((string)Eval("Video_Thumbnail")) %>'
CommandName="imgClick" CommandArgument='<%# Bind("Video_ID")%>'
BorderWidth="0px" Height="40px" ToolTip="Click to view video" Width="50px"
BorderStyle="Double" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnk_VideoName" runat="server" ToolTip="Click to view video"
CommandName="lnkClick" CommandArgument='<%# Bind("Video_ID")%>'
Text='<%# DataBinder.Eval(Container, "DataItem.Video_Name") %>'
CssClass="linkVideo" Width="130px"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#cccccc" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>`
in code behind for binding grid,
DataTable dt1Video = new MDBusiness.MDSUser().GetVideo(intNetId, intChanId, intCatId);
grd_Video.DataSource = dt1Video;
grd_Video.DataBind();
up1.Update();
up2.Update();
Set AutoPostBack = "False" on your dropdownlist to stop the postback from occurring. Users will then be able to change the dropdownlist without anything happening.
You'll need to use AJAX with your dropdownlist if you want to be able to use it without the postback.
for a quick answer
Install ASP.NET AJAX library
Wrap the GridView in a UpdatePanel
Set the trigger to be the DropDownList
Done :)
Remember to see this Screencast... it is exactly what you are after!