When I perform a radiobuttonclick, I want to set a dropdownlist to become visible. The radiobutton and dropdownlist are within the same datagrid. I am not sure how to do this.
<asp:UpdatePanel ID="updatepanel" UpdateMode="conditional" runat="server">
<ContentTemplate>
<asp:DataGrid ID="DataGrid" AutoGenerateColumns = "false" CssClass="objectSubTitle" ItemStyle-Wrap="true" runat="server" OnItemCommand="handler" ><Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:RadioButton ID ="RadioButton1" Text="Yes" GroupName="creatingNewCard" OnCheckedChanged="RadioButtonYes" AutoPostBack="True" runat="server" />
<asp:DropDownList ID="DropDownList1" Visible="false" runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
Assuming that they are in an ItemTemplate of a TemplateField and you want to switch vivibility on serverside:
protected void RbCheckedChanged(Object sender, EventArgs e)
{
var radioButton1 = (RadioButton)sender;
var row = (GridViewRow)radioButton1.NamingContainer;
var dropDownList1 = (DropDownList)row.FindControl("DropDownList1");
dropDownList1.Visible = radioButton1.Checked;
}
Sample-GridView:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="Grid_RowDataBound"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RbCheckedChanged" AutoPostBack="true"></asp:RadioButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Edit: as you've edited your question to show that you really use a DataGrid instead of a GridView, the code is similar:
protected void RbCheckedChanged(Object sender, EventArgs e)
{
var radioButton1 = (RadioButton)sender;
var item = (DataGridItem)radioButton1.NamingContainer;
var dropDownList1 = (DropDownList)item.FindControl("DropDownList1");
dropDownList1.Visible = radioButton1.Checked;
}
You can call the Visible property of the DropdwonList in the radio buttons checked changed event like this
protected void RadioButton1_CheckedChanged(Object sender, EventArgs e)
{
var radioButton1= (RadioButton)sender;
var item = (DataGridItem)radioButton1.NamingContainer;
var dropDownList1 = (DropDownList)item.FindControl("DropDownList1");
dropDownList1.Visible = radioButton1.Checked ? true : false;
}
Related
I am trying to bind a dropdown which is in a grid but I am getting error.
<asp:GridView ID="grdddl" AutoGenerateColumns="false" ShowHeader="false" OnRowDataBound="grdddl_RowDataBound" ShowFooter="true" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlcommtype" SelectedValue='<%#Eval("ComPlanRoleDescr") %>' AutoPostBack="true" OnSelectedIndexChanged="ddlcommtype_SelectedIndexChanged" runat="server"></asp:DropDownList>
<asp:HiddenField ID="hdnid" Value='<%#Eval("ID") %>' runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlcommtypefooter" AutoPostBack="true" OnSelectedIndexChanged="ddlcommtypefooter_SelectedIndexChanged" runat="server"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void grdddl_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlcommtype = (DropDownList)e.Row.FindControl("ddlcommtype");
ddlcommtype.DataSource = listcommtype;
ddlcommtype.DataTextField = "ComPlanRoleDescr";
ddlcommtype.DataValueField = "ID";
ddlcommtype.DataBind();
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlcommtype = (DropDownList)e.Row.FindControl("ddlcommtypefooter");
ddlcommtype.DataSource = listcommtype;
ddlcommtype.DataTextField = "ComPlanRoleDescr";
ddlcommtype.DataValueField = "ID";
ddlcommtype.DataBind();
}
}
This Code is giving error:
'ddlcommtype' has a SelectedValue which is invalid because it does not exist in the list of items.
In your GridView you are setting a SelectedValue for your DropDownList, but
the value cannot be found in the ListItem's that belong to the DropDownList.
Remove the SelectedValue from this line:
<asp:DropDownList ID="ddlcommtype" SelectedValue='<%#Eval("ComPlanRoleDescr") %>' AutoPostBack="true" OnSelectedIndexChanged="ddlcommtype_SelectedIndexChanged" runat="server"></asp:DropDownList>
If you do want to use a SelectedValue, why not do it after DataBind()? For example:
DropDownList ddlcommtype = (DropDownList)e.Row.FindControl("ddlcommtype");
ddlcommtype.DataSource = listcommtype;
ddlcommtype.DataTextField = "ComPlanRoleDescr";
ddlcommtype.DataValueField = "ID";
ddlcommtype.DataBind();
// Now set the default value:
ddlcommtype.SelectedValue = "InsertValueHere";
I'm trying to write a code that would update only one record for the data.
I do not need to be able to update every field on the row, only one particular field.
I have page that defines a GridView as following:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<UpdatePanel>
<ContentTemplate>
<fieldset style="width: 1%">
<legend>Search Customer</legend>
<table>
<tr>
<td>
<asp:DropDownList id="ddlSearchOption"
AutoPostBack="True"
OnSelectedIndexChanged="ddlSearchOption_SelectedIndexChanged"
runat="server">
<asp:ListItem Selected="True" Value="SearchBy"> Search By </asp:ListItem>
<asp:ListItem Value="CustID"> Customer ID </asp:ListItem>
<asp:ListItem Value="CustFirst"> First Name </asp:ListItem>
<asp:ListItem Value="CustLast"> Last Name </asp:ListItem>
<asp:ListItem Value="CustCity"> City </asp:ListItem>
</asp:DropDownList>
</td>
<asp:Panel id="pnlCustSearch" runat="server" >
<td>
<asp:Label id="lblEntry" runat="server" Text="" width="120px"></asp:Label>
</td>
<td>
<asp:TextBox id="txtSearchOptions" runat="server" width="50px">Hello</asp:TextBox>
</td>
<td>
<asp:Button id="btnFind" Text="Search" runat="server" onClick="btnFind_Click"></asp:Button>
</td>
</asp:Panel>
</tr>
</table>
</fieldset>
<div id ="msg">
<asp:Label id="lblMsg" runat="server" Text=""></asp:Label>
</div>
<div id="gridShow">
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"
AlternatingRowStyle-BackColor="#EEEEEE" EditRowStyle-BorderColor="Red">
<Columns>
<asp:TemplateField Visible="true" HeaderText="Customer ID">
<ItemTemplate>
<asp:Label runat="server" ID="lblCustID" Text='<%#Eval("CustID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label runat="server" ID="lblFirstName" Text='<%#Eval("CustFirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label runat="server" ID="lblLastName" Text='<%#Eval("CustLastName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label runat="server" ID="lblCity" Text='<%#Eval("CustCity") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtEmail" Text='<%#Eval("CustEmail") %>' />
<asp:Button runat="server" ID="btnUpdate" Text="Update" onClick="GridView1_btnUpdate_Click"/>
<asp:RequiredFieldValidator runat="server" ID="rfdCountry" ControlToValidate="txtEmail"
ValidationGroup="var1" ErrorMessage="*" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
</UpdatePanel>
</asp:Content>
Then in codebehind I write the following:
protected void GridView1_btnUpdate_Click(Object sender, EventArgs e)
{
//Code goes here
}
When clicking button to update email nothing happens.
What am I doing wrong?
I tried to use one of suggestions here:
if (!IsPostBack)
{
GridView1.DataSource = App_Data.DataHandler.GetData("fname", "de");
GridView1.DataBind();
GridView1.Visible = true;
}
The data is displayed but the click event does not work anyway
I added the following to my GridView definition:
onrowcommand="GridView1_RowCommand"
and added the code suggested:
<asp:Button runat="server" ID="btnUpdate" Text="Update" CommandName="UpdateEmail"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
In my .cs file I added the following:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UpdateEmail")
{
//Get the index from the argument
int index = Convert.ToInt32(e.CommandArgument);
//Get the row
GridViewRow row = GridView1.Rows[index];
//Do whatever you need with your row here!
}
}
Still, click event does not fire.
Ok, here is my code behind .cs file:
public partial class index : System.Web.UI.Page
{
protected override object LoadPageStateFromPersistenceMedium()
{
return Session["__VIEWSTATE"];
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
Session["VIEWSTATE"] = viewState;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Reset();
else
lblMsg.Text = "";
}
protected void Reset()
{
pnlCustSearch.Visible = false;
GridView1.Visible = false;
lblMsg.Text = "";
}
protected void ddlSearchOption_SelectedIndexChanged(object sender, EventArgs e)
{
Reset();
String ddlSelected = ddlSearchOption.SelectedValue;
if (ddlSelected != "")
{
ViewState["ddlSelected"] = ddlSelected;
pnlCustSearch.Visible = true;
SelectLabel(ddlSelected);
}
}
protected void SelectLabel(String choice)
{
switch (choice)
{
case "CustID":
lblEntry.Text = "Enter Customer ID";
break;
case "CustFirst":
lblEntry.Text = "Enter First Name";
break;
case "CustLast":
lblEntry.Text = "Enter Last Name";
break;
case "CustCity":
lblEntry.Text = "Enter City";
break;
default:
lblEntry.Text = "Enter Customer ID";
break;
}
}
protected void btnFind_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
String option = (String)ViewState["ddlSelected"];
String input = txtSearchOptions.Text.Trim();
GridView1.DataSource = DataHandler.GetData(option,input);
GridView1.DataBind();
GridView1.Visible = true;
}
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UpdateEmail")
{
//Get the index from the argument
int index = Convert.ToInt32(e.CommandArgument);
//Get the row
GridViewRow row = GridView1.Rows[index];
//Do whatever you need with your row here!
}
}
protected void btnUpdate_Click(Object sender, EventArgs e)
{
String txt = null;
txt = "here";
}
}
When you try to handle events from GridView, you need to use a CommandName and a CommandArgument then handle the RowCommand event from the GridView.
See http://msdn.microsoft.com/... for reference.
ASP.Net page should look like :
<asp:ButtonField runat="server" ID="btnUpdate" Text="Update"
CommandName="UpdateEmail"
CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" />
In the RowCommand event :
if (e.CommandName == "UpdateEmail")
{
//Get the index from the argument
int index = Convert.ToInt32(e.CommandArgument);
//Get the row
GridViewRow row = GridView1.Rows[index];
//Do whatever you need with your row here!
}
I got it, finally. I had to change <asp:Button/> to
`<asp:ButtonField Text="Update Email" CommandName="UpdateEmail"`
and remove it from ItemTemplate.
But why this was a problem. What I wanted to do is just to display textfield in the same itemtemplate with button?
Actually, the answer was much easier then I thought originally. I just used different values to set session: __VIEWSTATE and VIEWSTATE
protected override object LoadPageStateFromPersistenceMedium()
{
return Session["__VIEWSTATE"];
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
Session["VIEWSTATE"] = viewState;
}
As soon as I changed it, button was able to fire events.
Thank you
I'm having a problem calling a modal in asp
I need to set the postbackurl of linkbutton4 from code behind depending on what is selected in the dropdownlist! I have tried putting the postbackurl directlty on the linkbuttons tag it worked but when i change it from the code behind it doesnt BTW i change it when the link button is clicked.
Code behind for the linkbutton:
protected void LinkButton4_Click(object sender, EventArgs e)
{
var a = (Control)sender;
GridViewRow row = (GridViewRow)a.NamingContainer;
string b = row.Cells[0].Text;
Session["C"] = b;
DropDownList ddl =(DropDownList)row.Cells[7].FindControl("DropDownList1");
Session["D"] = ddl.SelectedItem.Text;
LinkButton lb = (LinkButton)row.Cells[7].FindControl("LinkButton4");
if (Session["D"].ToString() == "Upload")
{
lb.PostBackUrl = "preprod_design.aspx#edit";
// Upload();
}
if (Session["D"].ToString() == "Download")
{
Download();
}
infogridbind();
}
Here is the code for aspx :
<asp:GridView ID="GridView2" runat="server" ondatabound="GridView2_DataBound"
onrowdatabound="GridView2_RowDataBound"
onrowcreated="GridView2_RowCreated"
onselectedindexchanged="GridView2_SelectedIndexChanged"
onrowcommand="GridView2_RowCommand" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="SizeSetID" SortExpression="SizeSetID"/>
<asp:BoundField DataField="Revision No." SortExpression="RevisionNo" HeaderText = "Revision No."/>
<asp:TemplateField HeaderText ="Image">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" onError = "this.style.display = 'none';" ImageUrl='<%#"~/ClientPoImage.ashx?autoId="+Eval("[SizeSetID]")%>' Width="50px" Height="40px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Size Name" SortExpression="SizeName" HeaderText = "Size Name"/>
<asp:BoundField DataField="Quantity Requested" SortExpression="QuantityRequested" HeaderText ="Quantity Requested"/>
<asp:BoundField DataField="Quantity Received" SortExpression="QuantityReceived" HeaderText="Quantity Received"/>
<asp:BoundField DataField="Balance" SortExpression="Balance" HeaderText="Balance"/>
<asp:TemplateField HeaderText="Action">
<ItemTemplate >
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Upload</asp:ListItem>
<asp:ListItem>Download</asp:ListItem>
<asp:ListItem>Edit</asp:ListItem>
<asp:ListItem>Delete</asp:ListItem>
<asp:ListItem>Request</asp:ListItem>
<asp:ListItem>Receive</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="LinkButton4" runat="server" onclick="LinkButton4_Click">GO</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can change PostBackUrl for LinkButton inside DropDownList.SelectedIndexChanged event like this
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
var row = (GridViewRow)(ddl.NamingContainer);
var lb = (LinkButton)row.FindControl("LinkButton4");
if (ddl.SelectedValue == "Upload")
{
lb.PostBackUrl = "preprod_design.aspx#edit";
}
if (ddl.SelectedValue == "Download")
{
....
}
}
also you need change markup like this
....
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
....
Remove AutoPostBack="True" From DropDownList, and in the header of page <%# Page Title="data"... EnableEventValidation="false" %>
After That You just go to click event of Link button and then
GridViewRow gr = (GridViewRow)(((LinkButton)sender).NamingContainer);
DropDownList ddl = (DropDownList)gr.FindControl("DropDownList1");
If(ddl.SelectedValue =="Upload") // or u can use ddl.SelectedItem.Text
{
//Upload();
}
else if(ddl.SelectedValue == "Download")
{
//Download();
}
I have trying to Edit and Update my GridView in bulk.
I have generated a CheckBox in the first column of my GridView. It works something like this:
If I check a particular row in the GridView, the row gets editable.
Like this I can check as many rows I want to edit the GridView.
I have given a universal button called UPDATE, once after editing all the rows, and click on this button, the GridView is updated looping in each row checking for CheckBox.Check.
The issue I am facing here is that, when I click on any CheckBox in the GridView row, I am not getting TextBox.
I am trying to convert Label to TextBox on checking a CheckBox in the GridView.
So when I check a row, the text corresponding to Label template for that cell goes invisible according to my program, but fails to get the TextBox with the value of that cell.
The code I have tried is this:
protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = (sender as CheckBox);
if (chk.ID == "chkAll")
{
foreach (GridViewRow row in GridView3.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
}
}
}
CheckBox chkAll = (GridView3.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = true;
foreach (GridViewRow row in GridView3.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 3; i < row.Cells.Count; i++)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)//this condition is not satisfying when I debug the program. what is wrong in this line?
{
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
}
if (isChecked && !isUpdateVisible)
{
isUpdateVisible = true;
}
if (!isChecked)
{
chkAll.Checked = false;
}
}
}
}
UpdateGrid.Visible = isUpdateVisible;
}
The aspx code is:
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False"
DataKeyNames="Location_Profile_Name">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location_Profile_Name" SortExpression="Location_Profile_Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Location_Profile_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Home_Profile" SortExpression="Label10">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>
I have commented the issue I am facing in the program above. Kindly help.
I see that you are mixing to approaches, the Template approach (ItemTemplate, EditTemplate), and some Code-Behind approach (doing things hard-coded in code behind).
For your cenario, i sugest do it in code-behind, to get full control of the situation.
i addapted your code to explain my sugestion. Here is:
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location_Profile_Name" SortExpression="Location_Profile_Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Location_Profile_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Home_Profile" SortExpression="Label10">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:Label>
<%--Here are the controls that edit this row, we will handle this on code-behind--%>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Home_Profile") %>' Visible="false"></asp:TextBox>
<asp:Button ID="btnSave" Text="save" runat="server" Visible="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The "EditTemplate" was deleted, YOU will handle your own edit template.
protected void OnCheckedChanged(object sender, EventArgs e)
{
//... Your code ...
// Here we find the controls tha we will handle
CheckBox chkAll = (GridView3.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = true;
foreach (GridViewRow row in GridView3.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox CheckBox1 = (CheckBox)row.FindControl("CheckBox1");
Label Label2 = (Label)row.FindControl("Label2");
TextBox TextBox1 = (TextBox)row.FindControl("TextBox1");
Button btnSave = (Button)row.FindControl("btnSave");
//GridView3.SetEditRow(row.RowIndex);
if (CheckBox1 != null)
{
if (CheckBox1.Checked)
{
if (TextBox1 != null && Label2 != null)
{
// Shows your "Edit Template"
btnSave.Visible = true;
Label2.Visible = false;
TextBox1.Visible = true;
TextBox1.Text = Label2.Text;
}
}
}
}
}
}
for now, you have the control of situation :)
see this post Edit Update Delete Multiple Records
basically you need to add in your gridview
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server"
AutoPostBack="true"
OnCheckedChanged="chkSelect_CheckedChanged"/>
</ItemTemplate>
and code behind:
protected void chkSelect_CheckedChanged
(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
TextBox txtname = (TextBox)grdRow.FindControl("txtName");
if (chkTest.Checked)
{
txtname.ReadOnly = false;
}
else
{
txtname.ReadOnly = true;
}
}
if you familiar with Jquery you can easily enable the fields also in client side
Since AutoGenerateEditButton = false, therefore there should be no for Home_Profile. The TextBox template for Home_Profile should be written within and the visibility should be set to false.
The aspx code is:
<asp:TemplateField HeaderText="Home_Profile" SortExpression="Label10">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Home_Profile") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
i don't have the code for the way i previously did it, here is a little example using similar logic that what you did, i hope it helps
here is an working example doing it the way you did it
Code Behind
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr[0] = "Test " + i;
dt.Rows.Add(dr);
}
rptITem.DataSource = dt;
rptITem.DataBind();
}
}
protected void UpdateCB(object sender, EventArgs e)
{
foreach (RepeaterItem Item in rptITem.Items)
{
CheckBox cb = (CheckBox)Item.FindControl("cbTest");
TextBox tb = (TextBox)Item.FindControl("tbTest");
Label lb = (Label)Item.FindControl("lbTest");
tb.Visible = cb.Checked;
lb.Visible = !cb.Checked;
}
}
protected void UpdateAll(object sender, EventArgs e)
{
foreach (RepeaterItem Item in rptITem.Items)
{
CheckBox cb = (CheckBox)Item.FindControl("cbTest");
TextBox tb = (TextBox)Item.FindControl("tbTest");
Label lb = (Label)Item.FindControl("lbTest");
cb.Checked = true;
tb.Visible = true;
lb.Visible = false;
}
} }
Aspx Code
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Repeater ID="rptITem" runat="server">
<HeaderTemplate>
<div style="border:1px solid black">
<div style="width:50px; float:left"><asp:CheckBox ID="cbAll" runat="server" AutoPostBack="true" OnCheckedChanged="UpdateAll" /></div>
<div style="width:200px; float:left;">Name</div>
<div style="clear:both"></div>
</div>
</HeaderTemplate>
<ItemTemplate>
<div style="width:50px; float:left"><asp:CheckBox ID="cbTest" runat="server" AutoPostBack="true" OnCheckedChanged="UpdateCB" /></div>
<div style="width:200px; float:left;"><asp:Label ID="lbTest" runat="server" Text='<%# Eval("Name") %>' ></asp:Label></div>
<div style="width:200px; float:left;"><asp:TextBox ID="tbTest" runat="server" Text='<%# Eval("Name") %>' Visible="false"></asp:TextBox></div>
</ItemTemplate>
<SeparatorTemplate>
<div style="clear:both"></div>
</SeparatorTemplate>
</asp:Repeater>
</asp:Content>
In my GridView, I have the following columns:
<Columns>
<asp:BoundField DataField="report_type" HeaderText="Report Type"
SortExpression="report_type" />
<asp:BoundField DataField="progress" HeaderText="Progress"
SortExpression="progress" />
<asp:TemplateField HeaderText="..">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataValueField="progress">
<asp:ListItem Value="0">Incomplete</asp:ListItem>
<asp:ListItem Value="1">Complete</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
The progress column is just there for demo purposes which will eventually be removed. How do I get the value of the progress to select the correct itemlist in the dropdown?
So if the value of progress is 1, the dropdown should have Complete selected. If the value of the progress is 0, the dropdown should have Incomplete selected.
Add a OnRowDataBound attribute to the gridview in the .aspx page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" OnRowDataBound="GridViewRowEventHandler">
Replace
<asp:BoundField DataField="Progress" HeaderText="Progress"
SortExpression="progress" />
with
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="progress_Flags" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Progress").ToString()%>'/>
</ItemTemplate>
</asp:TemplateField>
In the code behind:
protected void GridViewRowEventHandler(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label flag = (Label)e.Row.FindControl("progress_Flags");
DropDownList myDropDown = (DropDownList)e.Row.FindControl("DropDownList1");
if (flag.Text == "1")
{
myDropDown.SelectedValue = "1";
}
//add more conditions here..
}
}
In RowDataBound event you can use e.Row.FindControl
protected void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
DataRowView dr = row.DataItem as DataRowView;
// now find the control in the row by control ID
DropDownList myDropDown = row.FindControl("DropDownList1") as DropDownList;
}