本文介绍了在asp.net中将复选字符串转换为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

* .aspx



 <   asp:templatefield     HeaderText   = 值 >  
< itemtemplate >
< asp:CheckBox ID = ckbValue runat = server 已选中 = ' <% #Eval( szValue)== DBNull.Value? false :Convert.ToBoolean(Eval( szValue))|| Eval( szDefaultValue)== DBNull.Value? false :Convert.ToBoolean(Eval( szDefaultValue))%> ' CssClass = pkchkbox > < / asp:CheckBox >
< / itemtemplate >
< / asp:templatefield >





* .aspx .cs

  foreach (DataRow row  in  m_dt.Rows)
{
if (row [ 1 ]。 ToString()== Y)
{
row [ 1 ] = true ;
}
else if (row [ 1 ]。ToString()== N)
row [ 1 ] = false ;
}





我要转换将显示在一个复选框中的szValue和szDefaultValue。

但它不起作用。

帮助我解决我的问题。 thankyou

解决方案



这不是很有用。你应该详细说明。



你的代码不健全,你可能需要处理 null 值,可能你有修剪检索到的字符串,可能你必须处理YN一切else



*.aspx

<asp:templatefield HeaderText="Value">
    <itemtemplate>
        <asp:CheckBox ID="ckbValue" runat="server" Checked='<%# Eval("szValue") == DBNull.Value ? false : Convert.ToBoolean(Eval("szValue")) || Eval("szDefaultValue") == DBNull.Value ? false : Convert.ToBoolean(Eval("szDefaultValue"))%>' CssClass="pkchkbox" ></asp:CheckBox>
    </itemtemplate>
</asp:templatefield>



*.aspx.cs

foreach (DataRow row in m_dt.Rows)
          {
              if (row[1].ToString() == "Y")
              {
                  row[1] = true;
                                  }
              else if (row[1].ToString() == "N")
                  row[1] = false;
          }



I wan to convert szValue and szDefaultValue that will display in one checkbox.
But it's not work.
Help me to solve my problem. thankyou

解决方案


That's not pretty informative. You should detail.

Your code is not robust, possibly you need to handle null values, possibly you have to trim the retrieved string, possibly you have to handle both "Y", "N" and everything else.



这篇关于在asp.net中将复选字符串转换为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:05