首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EditItemTemplate:条件绑定

EditItemTemplate:条件绑定
EN

Stack Overflow用户
提问于 2012-10-05 17:00:43
回答 2查看 6.5K关注 0票数 2

根据特定的行值(类型),我必须在EditTemplateField中使用TextBox或DropDownlist (只能使用其中之一)。如何在EditItemTemplate中有条件地绑定控件,以便告诉UpdateMethod应该考虑字段"Value“中的哪个控件?

代码语言:javascript
复制
<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”作为输入参数,它必须能够决定是从DropDownListValue还是从TextBoxValue获取它。

代码语言:javascript
复制
<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作为输入值(我猜是空字符串转换的结果)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-05 17:37:45

简单地使用Gridview的RowDatabound Event就可以解决你的problem...refer,代码如下……

代码语言:javascript
复制
 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);
                    }
               }

         }
     }
票数 1
EN

Stack Overflow用户

发布于 2012-10-05 17:35:12

尝尝这个

标记

代码语言:javascript
复制
<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>

代码隐藏

代码语言:javascript
复制
protected bool ShouldTextBoxBeVisible(object objType)
{
    return (objType != null && objType.ToString() == "TextBoxVisibleType");
}
protected bool ShouldDropDownBeVisible(object objType)
{
    return (objType != null && objType.ToString() == "DropDownVisibleType");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12742932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档