Datalist中单选按钮上的Eval ID

时间:2009-11-15 21:23:29

标签: radio-button asp.net datalist

我的代码有一个带有单选按钮的数据列表,iv使它成为单一可选的onitemdatabound ....现在我试图根据所选单选按钮评估一个隐藏字段

我的代码就像这样

aspx代码

<asp:DataList ID="DataList1" runat="server" RepeatColumns = "4"  CssClass="datalist1" 
                RepeatLayout = "Table"  OnItemDataBound="SOMENAMEItemBound"
                CellSpacing="20" onselectedindexchanged="DataList1_SelectedIndexChanged">
                <ItemTemplate>
                <br />
                <table cellpadding = "5px" cellspacing = "0" class="dlTable">
                <tr>
                <td align="center">
                <a href="<%#Eval("FilePath")%>" target="_blank"><asp:Image ID="Image1" runat="server" CssClass="imu" ImageUrl = '<%# Eval("FilePath")%>' 
                Width = "100px" Height = "100px" style ="cursor:pointer" />
                </td>            
                </tr>
                <tr >
                <td align="center">                    
                <asp:RadioButton ID="rdb" runat="server"  OnCheckedChanged="rdb_click" AutoPostBack="True" />            
                <asp:HiddenField ID="HiddenField1" runat="server" Value = '<%#Eval("ID")%>' /> 
                </td>
                </tr>                     
                </table>

 </ItemTemplate> 
</asp:DataList>

背后的代码

 protected void SOMENAMEItemBound(object sender, DataListItemEventArgs e)
        {
            RadioButton rdb;
            rdb = (RadioButton)e.Item.FindControl("rdb");
            if (rdb != null)
                rdb.Attributes.Add("onclick", "CheckOnes(this);");



        }

  protected void rdb_click(object sender, EventArgs e)
        {
            for (int i = 0; i < DataList1.Items.Count; i++)
            {
                RadioButton rdb;
                rdb = (RadioButton)DataList1.Items[i].FindControl("rdb");
                if (rdb != null)
                {
                    if (rdb.Checked)
                    {
                        HiddenField hf = (HiddenField)DataList1.Items[i].FindControl("HiddenField1");
                        Response.Write(hf.Value);
                    }

                }
            }
        }

即时通讯使用...

function CheckOnes(spanChk){


var oItem = spanChk.children;
var theBox= (spanChk.type=="radio") ?
spanChk : spanChk.children.item[0];

xState=theBox.unchecked;
elm=theBox.form.elements;

for(i=0;i<elm.length;i++)
if(elm[i].type=="radio" &&
elm[i].id!=theBox.id)
{
elm[i].checked=xState;
}
}

我得到这样的错误

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'pload Demonstration|'.

有没有其他方法可以做到这一点或者可以通过帮助来解决这个问题

2 个答案:

答案 0 :(得分:0)

问题可能是你的Response.Write()调用。看看this blog post。它概述了出现此特定异常的原因,以及如何防止它。另请查看this StackOverflow thread

答案 1 :(得分:0)

Okey让我们删除响应写入...现在我在我的代码隐藏中跟随...休息是相同的

label5.text=hf.value.ToString();

现在,当我使用更新面板和嵌套更新面板时,我可以评估Label

<asp:UpdatePanel ID="UpdatePanel9" runat="server" >
                <ContentTemplate>

                <asp:DataList ID="DataList1" runat="server" RepeatColumns = "4"  
                        CssClass="datalist1"  OnItemDataBound="SOMENAMEItemBound"
                CellSpacing="10" onselectedindexchanged="DataList1_SelectedIndexChanged" 
                        HorizontalAlign="Center" Width="500px">
                <ItemTemplate>
                <br />
                <table cellpadding = "5px" cellspacing = "0" class="dlTable">
                <tr>
                <td align="center">
                <a href="<%#Eval("FilePath")%>" target="_blank"><asp:Image ID="Image1" runat="server" CssClass="imu" ImageUrl = '<%# Eval("FilePath")%>' 
                Width = "100px" Height = "100px" style ="cursor:pointer" />
                </td>            
                </tr>
                <tr >
                <td align="center"> 
                    <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                    <Triggers>
                   <asp:PostBackTrigger ControlID="rdb" />
                    </Triggers>
                          <ContentTemplate>          
                <asp:RadioButton ID="rdb" runat="server"  OnCheckedChanged="rdb_click" AutoPostBack="true"  /> 

                 </ContentTemplate> 
                </asp:UpdatePanel>

                <asp:HiddenField ID="HiddenField1" runat="server" Value = '<%#Eval("ID")%>' /> 
                </td>
                </tr>                     
                </table>

 </ItemTemplate> 

</asp:DataList>


         </ContentTemplate>
         </asp:UpdatePanel>

有没有办法在没有完整页面回发的情况下完成此操作.... asyncpostback触发器似乎无法正常工作