Greetings!
I have a DropDownList within a FormView which are bound to XmlDataSources:
<asp:FormView ID="MyFormView" runat="server" DataSourceID="MyXmlDataSource">
<ItemTemplate>
<h1><%# XPath("SomeNode")%></h1>
<asp:Label ID="MyLabel" runat="server" AssociatedControlID="MyDdl" Text='<%# XPath("SomeOtherNode")%>' />
<asp:DropDownList ID="MyDdl"
runat="server"
DataSourceID="MyDdlDataSource"
DataTextField="name"
DataValueField="value"
AutoPostBack="true"
OnSelectedIndexChanged="MyDdl_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:FormView>
<asp:XmlDataSource ID="MyXmlDataSource" runat="server" XPath="Root/MainSection" />
<asp:XmlDataSource ID="MyDdlDataSource" runat="server" XPath="Root/MainSection/Areas/*" />
In the page's codebehind, I have the following OnLoad() method as well as the method for when the select index of the dropdownlist changes:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!IsPostBack)
{
string xml = GetMyXml(0); // default value
MyXmlDataSource.Data = xml;
MyDdlDataSource.Data = xml;
}
}
protected void MyDdl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList l_MyDdl = FindControl("MyDdl") as DropDownList;
int myVal;
if (l_MyDdl != null)
if (!Int32.TryParse(l_MyDdl.SelectedItem.Value, out myVal))
myVal = 0;
string xml = GetMyXml(myVal);
MyXmlDataSource.Data = xml;
MyDdlDataSource.Data = xml;
}
When a different value is selected from the dropdown list and SelectedIndexChanged is invoked, I am unable to get the value of the dropdown list (FindControl always returns null) in order to use it to re-bind the datasources. How can I get this value?
Because your dropdownlist is contained within another control it may be that you need a recursive findcontrol.
http://weblogs.asp.net/palermo4/archive/2007/04/13/recursive-findcontrol-t.aspx
Related
I have a drop down list in a repeater. I am trying to add a required field validator to it.
The aspx code is:
<asp:Repeater ID="myRepeter" runat="server" OnItemDataBound="myRepeter_ItemDataBound">
<ItemTemplate>
<asp:DropDownList ID="ddl_Name" runat="server" DataTextField="value" DataValueField="key" ></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfv_Name" ControlToValidate="ddl_Name" InitialValue="0" runat="server" ErrorMessage="Please select a Name" ValidationGroup="valgrp_Name" ForeColor="Red"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:Repeater>
I also tried the same from code behind:
protected void myRepeter_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
foreach(RepeaterItem item in myRepeter.Items)
{
DropDownList NametList = item.FindControl("ddl_Name") as DropDownList;
RequiredFieldValidator validator = item.FindControl("rfv_Name") as RequiredFieldValidator;
validator.ControlToValidate = NametList .ID;
validator.ValidationGroup = "valgrp_Name";
}
}
How can I add the required field validator?
Why are you looping through your repeater inside the databound event? It will automatically loop. Try using e.Item.FindControl instead.
DropDownList NametList = e.Item.FindControl("ddl_Name") as DropDownList;
RequiredFieldValidator validator = e.Item.FindControl("rfv_Name") as RequiredFieldValidator;
validator.ControlToValidate = NametList.ID;
validator.ValidationGroup = "valgrp_Name";
I'm having a repeater control and want to bind Images to the sc control for onitemdatabound event.
My markup is:
<sc:Link runat="server" ID="sclnk" Field="#" rel="iframe-960-540">
<sc:image id="scimage" runat="Server" field="#">
</sc:image>
</sc:Link>
And my code is:
Sitecore.Web.UI.WebControls.Link scBannerLink = e.Item.FindControl("sclnk") as Sitecore.Web.UI.WebControls.Link;
if (scBannerLink != null)
{
scBannerLink.DataBind(promoItem.ID.ToString(), promoItem.PromoLink.Field.InnerField.Name);
}
Sitecore.Web.UI.WebControls.Image scPromoImage = e.Item.FindControl("scimage") as Sitecore.Web.UI.WebControls.Image;
if (scPromoImage != null)
{
scPromoImage.DataBind(promoItem.ID.ToString(), promoItem.PromoImage.Field.InnerField.Name);
}
I'm not getting any error but not diaplaying images
I've never used the Databind method of the control to set the properties.
The easier solution is to specify the Fieldname and set the Item in your repeater:
<asp:Repeater runat="server" ID="rptImages">
<ItemTemplate>
<sc:Link runat="server" ID="scLnk" Field="MyLinkFieldName" Item="<%# Container.DataItem %>" Parameters="rel=iframe-960-540">
<sc:image id="scImage" runat="Server" Field="MyImageFieldName" Item="<%# Container.DataItem %>" />
</sc:Link>
</ItemTemplate>
</asp:Repeater>
And you can pass in the additional attributes in the Parameters field on the control as a URL encoded parameters string, e.g. Parameters="rel=iframe-960-540¶m2=value2¶m3=value3"
And your code behind binding the control should be:
protected void Page_Load(object sender, EventArgs e)
{
rptImages.DataSource = Sitecore.Context.Item.GetChildren(); // this needs to be changed to whatever your query is...
rptImages.DataBind();
}
I have a GridView that I have placed in DropDownList's in 2 columns.
<asp:TemplateField HeaderText="Upgrade" SortExpression="Upgrade">
<ItemTemplate>
<asp:Label ID="LabelUpgrade" runat="server" Text='<%# Eval("Upgrade") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlUpgrade" runat="server" Width="100px">
<asp:ListItem Value="1">--Select--</asp:ListItem>
<asp:ListItem Value="2">1</asp:ListItem>
<asp:ListItem Value="3">2</asp:ListItem>
<asp:ListItem Value="4">3</asp:ListItem>
<asp:ListItem Value="5">4</asp:ListItem>
<asp:ListItem Value="6">5</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
how do I grab the item from ddlUpgrade in the codebehind?
OnUpdating Event - I don't have a way to pull the row to get the value from the drop down but I add my sql parameters here.
protected void IAP_Updating(object sender, SqlDataSourceCommandEventArgs e){}
RowUpdating Event - I can get the row here but I can't add the value to the sql parameters because e.command isn't valid here
protected void gvClients_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow _row = gvClients.Rows[e.RowIndex];
DropDownList _ddl = (DropDownList)_row.FindControl("ddlUpgrade");
SqlParameter _parm = new SqlParameter("#Upgrade", _ddl.SelectedItem.ToString());
}
On the RowUpdating event you can capture the control inside the edit template based on its ID.
GridViewRow row = GridView1.Rows[e.RowIndex];
DropDownList ddl = (DropDownList)row.FindControl("ddlUpgrade");
SqlParameter _parm = new SqlParameter("#Upgrade", ddl.SelectedItem.ToString());
e.Command.Parameters.Add(_parm);
I would add a hidden field outside the GridView:
<asp:HiddenField ID="hdnSelection" value="" runat="server" />
And change the gvClients_RowUpdating method:
protected void gvClients_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow _row = gvClients.Rows[e.RowIndex];
DropDownList _ddl = _row.FindControl("ddlUpgrade") as DropDownList;
if(_ddl != null)
{
hdnSelection.Value = _ddl.SelectedItem.Text;
IAP.Update();//Assuming IAP is the ID of the SqlDataSource
}
}
And my IAP_Updating method should look like this:
protected void IAP_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
SqlParameter _parm = new SqlParameter("#Upgrade", hdnSelection.Value);
e.Command.Parameters.Add(_parm);
}
I did not test the code. You may need to tweak.
I have page with listview in it. There is label and dropdownlist in listview. I would like to access the text of label from ddlTags_Init() method.
Code:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="id_Image" onitemdatabound="ListView1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="TagsLabel" runat="server" Text='<%# Eval("Tags") %>' />
<asp:DropDownList ID="ddlTags" runat="server" OnInit="ddlTags_Init" >
</asp:DropDownList>
</ItemTemplate>
</asp:ListView>
Code behind:
protected void ddlTags_Init(object sender, EventArgs e)
{
DropDownList ddlTags = (DropDownList)sender;
Label lblTag = (Label)ddlTags.Parent.FindControl("TagsLabel");
string text=lblTag.Text;
}
At the moment i am stuck with
Label lblTag = (Label)ddlTags.Parent.FindControl("TagsLabel");
Anyone knows what am i missing?
Thanks, Jim
Assuming that there are more than 1 elements in the listview datasource, why don't you put your code in the ItemDataBound handler? I think that it should work.
Init is too early to get the bind value of Label. In other words, label value hasn't been bind yet.
Instead you might want to consider using ItemDataBound method.
<asp:ListView ID="ListView1" runat="server"
OnItemDataBound="ListView1_ItemDataBound" ...>
....
</asp:ListView>
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var ddlTags = e.Item.FindControl("ddlTags") as DropDownList;
var tagsLabel = e.Item.FindControl("TagsLabel") as Label;
}
}
I am having trouble getting my dropdownlist to populate after I update the sqldatasource or change FormView Modes. The dropdown is created from an array in the code behind. I will post the snips of code below. The dropdown binds as expected until these events.
Any assistance in why this does not work would be awesome.
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="tbl_PreRegistration"
Width="100%" CssClass="c2wForm" DefaultMode="Edit">
<EditItemTemplate>
<asp:DropDownList ID="stateDDL" runat="server" OnSelectedIndexChanged="State_DDL_SelectedIndexChanged"
CausesValidation="false" AutoPostBack="true">
</asp:DropDownList>
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" CssClass="button blue" />
<asp:LinkButton ID="btnReset" runat="server" CausesValidation="False"
Text="Cancel" CssClass="button white" OnClick="btnReset1_Click" />
</ContentTemplate></asp:UpdatePanel>
</EditItemTemplate>
</asp:FormView>
CODE BEHIND:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
populateStateDDL("stateDDL", "CA");
}
}
protected void populateStateDDL(string DDL_ID, string getCurrentValue)
{
DropDownList strDDL_ID = (DropDownList)FormView1.FindControl(DDL_ID);
ArrayList states = new ArrayList();
strDDL_ID.DataValueField = "Value";
strDDL_ID.DataTextField = "Text";
strDDL_ID.DataSource = formating.GetAllStates();
strDDL_ID.DataBind();
strDDL_ID.SelectedValue = getCurrentValue.ToUpper();
}
you need to create the control every time, not just when postback = false. the control should be rendered in the Init event so that it can then be wired into viewstate and all that other webforms stuff.
here is some pseudo-code
private DropDownList ctrl;
protected override void Init(EventArgs e)
{
base.Init(e);
ctrl = new DropDownList
{
Id = "name of control",
DataValueField = "Value",
DataTextField = "Text"
};
Controls.Add(ctrl);
}
protected override void Load(EventArgs e)
{
base.Load(e);
if(ispostback) return;
ctrl.DataSource = GetData();
DataBind();
}