I am using a gridview which is having dropdownlist in edittemplate field. There are 3 list items in dropdown : Red,Amber,Green. Instead of displaying text in listitems, I want to show the colors, for the same I am using dropdownlist's onLoad event, however this event is not able to recognize the dropdownlist.
Dropdownlist Designer code :
<asp:TemplateField HeaderText="Color">
<EditItemTemplate>
<asp:DropDownList ID="ddlcolor" runat="server" AppendDataBoundItems="true" DataTextField="COLOR" DataValueField ="COLOR" OnLoad="DDLColor_Load">
<asp:ListItem Value="-1">- Select Color -</asp:ListItem>
<asp:ListItem Value="0">Amber</asp:ListItem>
<asp:ListItem Value="1">Green</asp:ListItem>
<asp:ListItem Value="2">Red</asp:ListItem>
</asp:DropDownList></EditItemTemplate></asp:TemplateField>
Dropdownlist onLoad Event in codebehing :
protected void DDLColor_Load(object sender, EventArgs e)
{
for (int i = 0; i < ddlcolor.Items.Count; i++)
{
ddlcolr.Items[i].Attributes.Add("style", "background-color:" + ddlcolor.Items[i].Text);
}
}
However, it shows that the dropdownlist ddlcolor does not exists in current context.
Do I need to find this control in gridview ? Please suggest.
You are right. You need to find the control. Using the sender argument will help you locate the drop down without using the find control method
protected void DDLColor_Load(object sender, EventArgs e)
{
DropdownList ddlcolr=(Dropdownlist)sender;
for (int i = 0; i < ddlcolor.Items.Count; i++)
{
ddlcolr.Items[i].Attributes.Add("style", "background-color:" + ddlcolor.Items[i].Text);
}
}
Related
Here is my aspx code:
<EditItemTemplate>
<asp:DropDownList ID="ddlTotalColumn" runat="server">
<asp:ListItem Value="">Select value</asp:ListItem>
<asp:ListItem Value="0">1</asp:ListItem>
<asp:ListItem Value="1">2</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
My aspx.cs code:
protected void gvTest_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow selected_row = gvTest.Rows[e.RowIndex];
var total_column_drop_down_list = (DropDownList)selected_row.FindControl("ddlTotalColumn");
int column_string = Convert.ToInt32(total_column_drop_down_list.SelectedItem.Value);
gvTest.EditIndex = -1;
...
}
At this line: int column_string = Convert.ToInt32(total_column_drop_down_list.SelectedItem.Value);
I have an error: "Input string was in incorrect format" because "total_column_drop_down_list.SelectedItem.Value" will return empty string ("").
So is there any bright idea?
It sounds like you have made the classic mistake of not putting your databinding code inside a if (!Page.IsPostBack) block. Thus, your GridView re-binds, and you get default values in your RowUpdating event (rather than what you selected).
Wherever you are binding your GridView, in Page_Load for instance, you need to do this:
if (!Page.IsPostBack)
{
BindGrid();
}
Where "BindGrid()" is whatever code you call to databind your GridView.
On a slightly unrelated note, you can actually use the GridViewUpdateEventArgs parameter that's passed to that method to grab the updated values (rather than using FindControl to get the DropDownList, and then getting the values).
I have created a form for saving Medium details, columns are, MID,MediumName,CreationDate,Status.I have bind full table in a gridview name gvmedium, i am having a textbox on the form .I want to fill the text box from grid views value of column MediumName on the template button click event.can any one help me?
You can use CommandName and Command Argument to achieve it and on Code Behind use RowCommand event.
<ItemTemplate>
<asp:LinkButton ID="lnkResetPassword" Text="Reset Password" runat="server" CommandName="ResetPassword" CommandArgument='<%# Bind("UserId") %>'></asp:LinkButton>
</ItemTemplate>
and then do your work here
protected void grdUserGroupList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ResetPassword")
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
lblMId.Text = e.CommandArgument.ToString();
txtMedium.Text=gvMedium.Rows[RowIndex ].Cells[0].Text;
}
}
I have nested repeaters on my aspx page.In the outer repeater I am displaying a list of products and in the inner repeater I am displaying a list of additional options associated with each product.The inner repeater contains a checkbox,textbox,label and other stuff.I would like to find the controls inside the outer repeater when a user selects a checkbox in the inner repeater.In order to handle this I am using the following code.
<asp:Repeater ID="OuterRepeater" runat="server"
onitemdatabound="OuterRepeater_ItemDataBound" >
<ItemTemplate>
<asp:Label ID="CodeLabel" runat="server" Text='<%# Eval("Code") %>'></asp:Label>
<asp:Repeater ID="InnerRepeater" runat="server" OnItemCreated="InnerRepeater_ItemCreated">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
......
.......
</ItemTemplate>
</asp:Repeater>
......
......
</ItemTemplate>
</asp:Repeater>
protected void InnerRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
{
RepeaterItem ri = (RepeaterItem)e.Item;
if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem
)
{
CheckBox cb = ri.FindControl("CheckBox1") as CheckBox;
cb.CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);
}
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
if (cb.Checked)
{
//do something
}
else
{
//do something
}
}
But the checkedChanged event of the checkbox is not firing for some reason.Also I am not sure how to access the textbox of the outer repeater in the checked changed event of the innter repeater checkbox control.
Could someone please help me with this?
Thanks
It does not fire the CheckedChanged event, since you have declared the event handler as private, You have to make it Protected or Public
Protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
You can access the Textbox control like..
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
Textbox textbox1 = (TextBox)checkBox.Parent.FindControl("TextBox1");
String textboxText = textbox1.Text;
}
It doesn't look like you defined an event handler in your markup.
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
Muhammad Akhtar's answer helped me a lot today!
I just needed to set a specific ID to my dynamic generated checkboxes inside my reapeater to recover the origin of the event, and do the rest of the processing, and it worked perfectly.
chkAtivo.ID = DataBinder.Eval(e.Item.DataItem, "id").ToString();
Reovered just as the sample.
Cant vote up yet, but thank you.
In my project i have kept source info in dropdownlist. Like source of info: in dropdownlist three items website, Newspaper and others. If user select Others item, then only other text box should be visible otherwise should be invisible. For that i have set in page load event
lblother.visible=false;
txtother.visible=false;
And in Btnsubmit event i have written the condition like.
if(dropdownlistinfo.selectedindex==2)
{
lblother.visible=true;
txtother.visible=true;
}
But in my case i m not getting my desire output. Its always invisible when i am selecting Others item from drowdownlist also. Pls somebody help me where is my mistake?
Thanks,
Sumit
I think the problem is here.
if (!IsPostBack)
{
lblother.visible = false;
txtother.visible = false;
}
This will work if you set Selected property of the default list item.
<asp:DropDownList ID="DropDownList" runat="server">
<asp:ListItem Text="Website" Selected="True"></asp:ListItem>
<asp:ListItem Text="Newspaper"></asp:ListItem>
<asp:ListItem Text="Other"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblOther" runat="server" Text="Other"></asp:Label>
<asp:TextBox ID="txtOther" runat="server"></asp:TextBox>
Hide the controls in the Page Load event.
protected void Page_Load(object sender, EventArgs e)
{
this.txtOther.Visible = false;
this.lblOther.Visible = false;
}
Then show the controls in the button click event.
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 2)
{
this.txtOther.Visible = true;
this.lblOther.Visible = true;
}
}
in the modal pop up i am using detailsview control by default all the data would
be shown in label.
there will be an edit button down once the user clicks edit button all the lablel would be gone and text box and dropdown control should be present so that user can change the values and again update into database
looking forward for a solution. i dnt want to use sqlDatasource. i wanted it to do in .cs
thank you
here is how to:
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" />
</EditItemTemplate>
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
DropDownList ddl = DetailsView1.FindControl("DropDownList1") as DropDownList;
if (ddl != null)
{
ddl.DataSource = dataSource;
ddl.DataBind();
}
}
}