2

特定の行の値(タイプ)に応じて、EditTemplateField内でTextBoxまたはDropDownlistを使用する必要があります(そのうちの1つのみ)。「値」フィールドで考慮されるコントロールをUpdateMethodに指示するために、EditItemTemplate内で条件付きコントロールをバインドするにはどうすればよいですか?

<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>

GridViewのUpdateMethodには、入力パラメーターとして「Value」があり、DropDownListValueまたは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>

コントロールTextBoxValueとDropDownListValueを(プロパティ "Visible"を使用して)非表示/表示しようとしましたが、機能しません。UIは正常ですが、UpdateMethodは常に入力値として0を受け取ります(空の文字列キャストの結果だと思います)。

4

2 に答える 2

1

Gridviewを使用するだけでRowDatabound Event、問題が解決するはずです...以下のコードを参照してください...

 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);
                    }
               }

         }
     }
于 2012-10-05T09:37:45.460 に答える
0

これを試して

マークアップ

<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>

コードビハインド

protected bool ShouldTextBoxBeVisible(object objType)
{
    return (objType != null && objType.ToString() == "TextBoxVisibleType");
}
protected bool ShouldDropDownBeVisible(object objType)
{
    return (objType != null && objType.ToString() == "DropDownVisibleType");
}
于 2012-10-05T09:35:11.713 に答える