Is this a FormView bug?

安稳与你 提交于 2019-12-11 07:57:21

问题


I have a FormView (with paging enabled) that is bound to a LinqDataSource on an ASP.NET page. I'm experiencing some very weird behavior and can't figure out why it's happening. For simplicity sake on this question I have removed some unneeded code (other FormView templates, etc) to demonstrate this behavior.

My FormView has 3 fields, two textboxes and one DropDownList. The DropDownList is bound to another LinqDataSource on the page and contains foreign key values. When the FormView's LinqDataSource only contains one record and I try to update it, the update fails because the selected value of the DropDownList is always empty, no matter which value I pick for it. When the FormView's LinqDataSource contains 2 or more records, it works as it should.

Now here's the really weird thing. The update is actually failing because of the FormView's PagerSettings! When I use just the default Pager settings, all is well. When I change the PagerMode to NextPreviousFirstLast, the update fails.

Here's my FormView with it's data sources:

<asp:FormView ID="fvData" runat="server" AllowPaging="True" 
    DataKeyNames="ID" DataSourceID="ldsData" DefaultMode="Edit">
    <EditItemTemplate>
        <table class="pad5">
            <tr>
                <td class="field-name">AREA:</td>
                <td>
                    <asp:DropDownList ID="cboAREA" runat="server" DataTextField="AREA_NAME" 
                        DataValueField="AREA1" SelectedValue='<%# Bind("AREA") %>' DataSourceID="ldsAreas" />
                </td>
            </tr>
            <tr>
                <td class="field-name">LOOP:</td>
                <td><asp:TextBox ID="txtLOOP" runat="server" Text='<%# Bind("LOOP") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">LOOP DESCRIPTION:</td>
                <td><asp:TextBox ID="txtLOOP_DESCRIPTION" runat="server" 
            Text='<%# Bind("LOOP_DESCRIPTION") %>' style="width: 600px" /></td>
            </tr>
        </table>

        <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" CausesValidation="True" />
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" />
    </EditItemTemplate>
    <PagerSettings Mode="NextPreviousFirstLast" 
        FirstPageText="&amp;lt;&amp;lt; First" LastPageText="Last &amp;gt;&amp;gt;" 
        NextPageText="Next &amp;gt;" PreviousPageText="&amp;lt; Prev" 
        Position="TopAndBottom" />
    <PagerStyle CssClass="pager" />
</asp:FormView>

<asp:LinqDataSource ID="ldsData" runat="server" 
    ContextTypeName="E_and_I.EAndIDataDataContext" EnableDelete="True" 
    EnableInsert="True" EnableUpdate="True" EntityTypeName="" 
    TableName="INSTRUMENT_LOOP_DESCRIPTIONs" onselecting="ldsData_Selecting" OrderBy="ID ASC" >
</asp:LinqDataSource>

<asp:LinqDataSource ID="ldsAreas" runat="server" 
    ContextTypeName="E_and_I.EAndIDataDataContext" EntityTypeName="" 
    TableName="AREAs" onselecting="ldsAreas_Selecting">
</asp:LinqDataSource>

And here's both of my LinqDataSource's Selecting events:

EAndIDataDataContext db = new EAndIDataDataContext();

protected void ldsData_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    e.Result = db.INSTRUMENT_LOOP_DESCRIPTIONs.Take(1); // we only want one record for testing
}

protected void ldsAreas_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    e.Result = db.AREAs.OrderBy(a => a.AREA1).Select(a => new { AREA1 = a.AREA1, AREA_NAME = "(" + a.AREA1 + ") " + a.AREA_NAME });
}

I've traced the problem to these lines:

<PagerSettings Mode="NextPreviousFirstLast" 
    FirstPageText="&amp;lt;&amp;lt; First" LastPageText="Last &amp;gt;&amp;gt;" 
    NextPageText="Next &amp;gt;" PreviousPageText="&amp;lt; Prev" 
    Position="TopAndBottom" />

As soon as I remove the above PagerSettings element, the FormView updates the record just fine! Does anybody know why the hell the pager settings would have anything to do with this? I'm using the .NET Framework 4.0.


回答1:


I copied part of your code and I made an experiment and I think, I'm experimenting the same behavior.

This is what I did:

<asp:LinqDataSource runat="server" ID="lds"
    TableName="jobs" ContextTypeName="WebApplication2.DataAccess.PubsDataContext" >

</asp:LinqDataSource>
<asp:LinqDataSource runat="server" ID="ldse"
    TableName="employee" ContextTypeName="WebApplication2.DataAccess.PubsDataContext" OnSelecting="ldse_Selecting">

</asp:LinqDataSource>

<asp:FormView runat="server" DefaultMode="Edit" ClientIDMode="Predictable" DataKeyNames="emp_id" DataSourceID="ldse"
    AllowPaging="true" OnItemCommand="Unnamed_ItemCommand" ID="formView">
    <EditItemTemplate>
        <div>
            <asp:DropDownList runat="server" ID="ddlJobs" DataSourceID="lds" DataTextField="job_desc" DataValueField="job_id">
            </asp:DropDownList>
        </div>
        <div>
            <asp:TextBox runat="server" TextMode="MultiLine" ID="txtDesc" />
        </div>
        <div>
            <asp:Button Text="Save" runat="server" CommandName="Save" CausesValidation="true" />
            <asp:Button Text="Cancel" runat="server" CommandName="Cancel" CausesValidation="false" />
        </div>
    </EditItemTemplate>
    <PagerSettings Mode="NextPreviousFirstLast" 
        FirstPageText="&amp;lt;&amp;lt; First" LastPageText="Last &amp;gt;&amp;gt;" 
        NextPageText="Next &amp;gt;" PreviousPageText="&amp;lt; Prev" 
        Position="TopAndBottom" />

These are my observations:

  • If the FormView contains only one row, then the FormView behaves weird and my commands do not work as expected.

    • My Save command is never fired, instead the `Cancel command is fired

    • My Cancel command causes my FormView to disappear...

First I thought this was because of the &amp; characters, but this is not the case

I narrowed the problem to Position="TopAndBottom"

  • If you set the Position attribute to:

    • Top

    • TopAndBottom

    You will experiment the same problem. (As you mention probably a bug)

  • If you set the Position attribute to:

    • Buttom

    It works as expected

To be honest, if this is a bug, I can't believe none else has found it and report it.

I made the same experiment with Visual Studio 2012 for Web (ASP.NET 4.5) and I'm experimenting the exact same behavior...

This makes me think that probably I'm doing something fundamentally wrong and perhaps this is not a bug and that's why I'm experimenting the same behavior with both versions ASP.NET 4 and ASP.NET 4.5, but if that's the case, I simply can't figure out what is it.

On the other hand, if this is a bug, the same bug will be present in ASP.NET 4.5

Edit 1

I uploaded the code to my GitHub site for reference




回答2:


It's most likely due to the url decoding of the &amp &lt and &gt.

Try taking out the &amp';' and just use the & symbol. You are able to inject using that method.



来源:https://stackoverflow.com/questions/12611347/is-this-a-formview-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!