Issue making textbox visible based on specific input from a radio button list in an EditItemTemplate

D

Dan

Issue making textbox visible based on specific input from a radio button
list in an EditItemTemplate

I want to setup a gridview that when in edit mode and when the user
selects "Other" from a radiolist that a text box will appear and the
user can input text.

I can get the textbox to appear after selecting other in the radiolist;
however, the selected value in the radiolist disappears. I believe this
is due to the databind call.

But I cannot seemd to get the textbox to appear without makeing the
databind call.

Can someone steer me in the right direction?

My sample code:

<asp:GridView DataKeyNames="ReturnId" ID="ListGrid" runat="server"
AutoGenerateColumns="False"
OnRowEditing="ListGrid_RowEditing"
OnRowUpdating="ListGrid_RowUpdating"
OnRowCancelingEdit="ListGrid_RowCancelingEdit">

<Columns>
<asp:BoundField DataField="ReturnId" HeaderText="Return Id"
ReadOnly="true" />

<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="UpdateBtn" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="CancelBtn" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="EditBtn" runat="server"
CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reason"
SortExpression="ReasonForCredit">
<EditItemTemplate>
<asp:RadioButtonList ID="ddlReason" AutoPostBack="true"
runat="server" OnSelectedIndexChanged="ddlReason_SelectedIndexChanged" <asp:ListItem Text="Other" Value="0" />
<asp:ListItem Text="Too big" Value="2" />
<asp:ListItem Text="Too small" Value="1" />
</asp:RadioButtonList>
<asp:TextBox ID="tbOther" Visible='<%# IsOtherTBVisible(
Eval("ReasonCode")) %>' runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblReasonCode" Text='<%#
Eval("ReasonCode") %>' runat="server" />
</ItemTemplate>
<ItemStyle CssClass="name" />
<HeaderStyle CssClass="name" />
</asp:TemplateField>

</Columns>

</asp:GridView>

code behind

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

protected void BindData()
{
if (!IsPostBack || _RefreshData)
{
DataSet ds = SqlTools.OpenDataSet("SELECT * FROM RETURNS",
ConnString);
DataView dv = ds.Tables[0].DefaultView;

ListGrid.DataSource = ds.Tables[0];
SaveDataSetToSession(ds);
_RefreshData = false;
}
else
{
DataView dv = RetrieveSessionDataSet().Tables[0].DefaultView;
ListGrid.DataSource = RetrieveSessionDataSet().Tables[0];
}

ListGrid.DataBind();
}

protected void SaveDataSetToSession(DataSet dsToSave)
{
Session.Add("Returns", dsToSave);
}

protected DataSet RetrieveSessionDataSet()
{
return (DataSet)Session["Returns"];
}

protected void ListGrid_RowEditing(object sender, GridViewEditEventArgs
e)
{
ListGrid.EditIndex = e.NewEditIndex;
BindData();
}

protected void ListGrid_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
ListGrid.EditIndex = -1;
BindData();
}

protected void ddlReason_SelectedIndexChanged(object sender, EventArgs
e)
{
if (((RadioButtonList)sender).SelectedItem.Value == "0")
{
_tbOtherVisible = true;
}
else
{
_tbOtherVisible = false;
}

BindData();
}

protected bool IsOtherTBVisible(object ReasonCode)
{
return _tbOtherVisible;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top