Pregunta

According to a specific row value (Type), I must use a TextBox or a DropDownlist inside an EditTemplateField (only ONE of them). How can I bind conditionally controls inside the EditItemTemplate in order to tell the UpdateMethod which is the control to take into consideration for the field "Value"?

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="LabelType" runat="server" Text='<%# Eval("Type") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:Label ID="LabelType" runat="server" Text='<%# Eval("Type") %>'></asp:Label>
    </EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
    <ItemTemplate>
        <asp:Label ID="LabelValue" runat="server" Text='<%# Eval("Value") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <div style="text-align:center">
            <asp:TextBox ID="TextBoxValue" runat="server" Text='<%# Bind("Value") %>'></asp:TextBox>
            <asp:DropDownList ID="DropDownListValue" runat="server" SelectedValue='<%# Bind("Value") %>'>
            </asp:DropDownList>
        </div>
    </EditItemTemplate>
</asp:TemplateField>

The GridView's UpdateMethod has "Value" as input parameter and it must be able to decide if taking it from DropDownListValue or TextBoxValue.

<asp:ObjectDataSource ID="ODSResults" runat="server" 
    SelectMethod="GetDataByIdDevice" 
    TypeName="DataSetSWCTableAdapters.DispositivoParametro_TableAdapter" 
    UpdateMethod="Save">
    <SelectParameters>
        <asp:QueryStringParameter Name="IdDevice" QueryStringField="id" Type="Int32" />
        <asp:ProfileParameter Name="Culture" PropertyName="Cultura" Type="String" />
        <asp:Parameter Name="ParameterCode" Type="String" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="IdDevice" Type="Int32" />
        <asp:Parameter Name="IdParameter" Type="Int32" />
        <asp:Parameter Name="Value" Type="Int64" />
    </UpdateParameters>
</asp:ObjectDataSource>

I tried to hide/show the controls TextBoxValue and DropDownListValue (using the property "Visible") but it does not work: UI is fine but the UpdateMethod always receives 0 as input value (I guess as result of an empty string casting).

¿Fue útil?

Solución

Simple use of RowDatabound Event of Gridview should solve your problem...refer the code below...

 protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
     {
         if (e.Row.RowType == DataControlRowType.DataRow)
         { 
            if( e.Row.RowState==DataControlRowState.Edit)
               {
                  DropDownList drpctrl =(DropDownList)e.Row.Cells[CellIndex].FindControl("DropDownListValue");
                  TextBox  txtCntrl=(TextBox )e.Row.Cells[CellIndex].FindControl("TextBoxValue");
                  if(YuorCondition)
                    {
                        drpctrl.Visible=true/false;
                        txtCntrl.Visible=true/false;

                        ODSResults.UpdateMethod = "Save";
                        ODSResults.InputParameters.Clear(); 
                        ODSResults.InputParameters.Add("IdDevice", "Value1");
                                  ODSResults.InputParameters.Add("IdParameter", "Value2");
                        ODSResults.InputParameters.Add("Value", dropdownValue/Textbox Value);
                    }
               }

         }
     }

Otros consejos

Try this

Markup

<asp:TextBox ID="TextBoxValue" runat="server" 
        Text='<%# Bind("Value") %>'
        Visible='<%# ShouldTextBoxBeVisible(Bind("Type")) %>'>
</asp:TextBox>
<asp:DropDownList ID="DropDownListValue" runat="server" 
        SelectedValue='<%# Bind("Value") %>'
        Visible='<%# ShouldDropDownBeVisible(Bind("Type")) %>'>
</asp:DropDownList>

Code-Behind

protected bool ShouldTextBoxBeVisible(object objType)
{
    return (objType != null && objType.ToString() == "TextBoxVisibleType");
}
protected bool ShouldDropDownBeVisible(object objType)
{
    return (objType != null && objType.ToString() == "DropDownVisibleType");
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top