标签: code-behind

<script runat ="server">与代码隐藏文件

<script runat='server'></script>在代码隐藏中,c#代码之间是否有任何缓存/性能/显着差异?

c# asp.net code-behind runatserver

1
推荐指数
1
解决办法
1570
查看次数

如何从代码隐藏中删除Html <li>项目符号?

从codebehind创建一个html列表,如下所示:

HtmlGenericControl list = new HtmlGenericControl("ul");
for (int i = 0; i < 10; i++)
{
   HtmlGenericControl listItem = new HtmlGenericControl("li");
   Label textLabel = new Label();
   textLabel.Text = "Menu"+i;
   listItem.Controls.Add(textLabel);
   list.Controls.Add(listItem);
}
Run Code Online (Sandbox Code Playgroud)

但渲染列表有子弹,我想阻止它们.

javascript css code-behind html-lists

1
推荐指数
2
解决办法
4434
查看次数

当DataTemplate没有键,只有TargetType时,从ResourceDictionary访问DataTemplate

我在XAML中用代码隐藏了一个ResouceDictionary。我需要使用鼠标事件和数据绑定定义一些特定于视图的行为,为此,我需要访问在DataTemplate内部定义的某些元素。

问题是,DataTemplate没有Key,而只有TargetType(这是必需的,因此WPF会自动将其用于给定类型)。

那么,如何从代码隐藏访问DataTemplate?

编辑:

如果在构造函数中的某个地方放置断点,则可以看到ViewModel的模板在那里。似乎ResourceDictionary.Keys属性是一个对象数组,而我要访问的键(实际上是相应的值)在调试器中是这样的:

{DataTemplateKey(Company.Application.ViewModels.TargetViewModel)}
Run Code Online (Sandbox Code Playgroud)

XAML:

<sys:Double x:Key="escala">10</sys:Double>
<sys:Double x:Key="raio">20</sys:Double>
<EllipseGeometry x:Key="geometriacirculo"
    RadiusX="{StaticResource raio}"
    RadiusY="{StaticResource raio}"/>
<ScaleTransform x:Key="transform" ScaleX="{StaticResource escala}" ScaleY="{StaticResource escala}" />
<ap:NormalConverter x:Key="NormalConverter"/>   
<ap:BitmapToSource x:Key="BitmapToSource"/>

<DataTemplate DataType="{x:Type vm:TelaColetaViewModel}">
        <.....
Run Code Online (Sandbox Code Playgroud)

代码背后:

public partial class TelaColetaTemplate : ResourceDictionary
{

    EllipseGeometry _geometria_circulo;
    ScaleTransform _scale_transform;
    Grid GridZoom;
    Path CirculoGuia;

    double _escala;

    Point? _ponto_clicado_norm;     

    public TelaColetaTemplate()
    {
        InitializeComponent();

        // three following lines work, accessing them with key, no problem
        _geometria_circulo = (EllipseGeometry)this["geometriacirculo"];
        _scale_transform = (ScaleTransform)this["transform"];
        _escala = (double)this["escala"];


        //var wantedTemplate = ???? …
Run Code Online (Sandbox Code Playgroud)

wpf code-behind resourcedictionary targettype

1
推荐指数
1
解决办法
998
查看次数

从代码隐藏格式化 DataGridTextColumn

也许,我想,事情太简单了。我有一个数据网格,从后面的代码动态添加列。这有效。然后我有带有整数值的列,我希望在列中看到右对齐,但它失败了。我的代码是:

    public List<List<int>> IntegerData { get; set; }................

            DataGridTextColumn textC = new DataGridTextColumn();
            textC.Header = ("Column");
            textC.Binding = new Binding(string.Format("[{0}]", i));    
            textC.Binding.StringFormat = "00000"; 
Run Code Online (Sandbox Code Playgroud)

最后我有类似的东西:

            myDataGrid.Itemssource = IntegerData
Run Code Online (Sandbox Code Playgroud)

使用 textC.Binding.String.Format 的问题是:(首先:看来,stringFormat-LIne 正确重载了前一行)

格式“0”给出一个整数“1”“2”,这是正确的。

格式“00000”给出“00001”“00002”“00003”,这对于字符串格式是正确的,但不是我想要的

但:

格式 "######" 也给出 "1" "2" .. 等等(左对齐,没有前导空格)

由于格式规则我期望(或希望), ##### 是所描述的占位符,结果是 "bbbb1" , "bbbb2" 右对齐。(b 在这里是空白!)

所以绑定似乎正确处理“00000”格式,但不能正确处理“######”格式。有人对这个想法有任何想法或经验吗?(我在互联网上找到的所有其他绑定要复杂得多)

c# wpf string.format code-behind datagridtextcolumn

1
推荐指数
1
解决办法
3839
查看次数

WPF 数据绑定组合框中的彩色项目

我读过其他几篇文章,但没有人能够回答我的问题组合
我有一个 ComboBox,我想在其中以不同颜色显示项目,这可以通过使用 ComboBoxItem 并设置其背景来完成。当我想以不同的颜色存储我的 CategoryDTO 并稍后能够再次提取它们时,我的问题就出现了。我需要显示的只是 CategoryDTO 的颜色和名称属性。然后我必须能够从 SelectedItem 属性获取 CategoryDTO 对象。我尝试了使用 ItemsSource、DisplayMemberPath 和 SelectedValuePath 的各种解决方案。但只完成了这个组合框的图片
正如所见,它显示颜色,但仅显示所选 CategoryDTO 的名称,我什至还没有测试 SelectedItem 是否正常工作。下面我将放置我使用的代码。

[Serializable]
public class CategoryDTO
{
    public string Name { get; set; }
    ...not important...
}


CategoryDTO[] categories = await _isd.GetCategoriesAsync();
comboBoxCategory.ItemsSource = categories.Select(c => new CategoryComboBoxItem(c)).ToList();
comboBoxCategory.DisplayMemberPath = "Name";
comboBoxCategory.SelectedValuePath = "Name";

public class CategoryComboBoxItem : ComboBoxItem
{
    public CategoryComboBoxItem(CategoryDTO category)
    {
        this.Background = new SolidColorBrush(category.Color);
        this.Content = category;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在 .xaml 中没有指定任何特殊内容,因此我将忽略该部分。除此之外,我希望能够使用 Name 属性设置 SelectedItem。我非常喜欢将答案放在代码隐藏中,但如果它非常复杂,则仅使用 .xaml …

c# data-binding wpf combobox code-behind

1
推荐指数
1
解决办法
3419
查看次数

“不存在数据时读取尝试无效”错误

我有这个代码块:

using (SqlConnection con2 = new SqlConnection(str2))
{
    using (SqlCommand cmd2 = new SqlCommand(@"SELECT * FROM VW_MOS_DPL_AccountValidation WHERE CUST_NUM = @CNum", con2))
    {
        con2.Open();

        cmd2.Parameters.AddWithValue("@CNum", TBAccountNum.Text);

        using (SqlDataReader DT2 = cmd2.ExecuteReader())
        {
            // If the SQL returns any records, process the info
            if (DT2.HasRows)
            {
                // If there's a BusinessID (aka Business Type), fill it in
                string BizID = (DT2["Business_ID"].ToString());
                if (!string.IsNullOrEmpty(BizID))
                {
                    DDLBustype.SelectedValue = BizID;
                }
            }
        }
        con2.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

当它到达线路时

string BizID = (DT2["Business_ID"].ToString());
Run Code Online (Sandbox Code Playgroud)

它抛出一个错误:

不存在数据时尝试读取无效。

if …

c# code-behind

1
推荐指数
1
解决办法
1255
查看次数

带入视图不起作用

我在事件处理程序后面有这个代码:

private void comboActiveStudentAssignmentType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    List<Border> borders = new List<Border>();

    // The list of border (focus rectangles) matches the combo of assignment types
    borders.Add(borderBibleReadingMain);
    borders.Add(borderBibleReadingClass1);
    borders.Add(borderBibleReadingClass2);
    borders.Add(borderMainHallStudent1);
    borders.Add(borderMainHallAssistant1);
    borders.Add(borderMainHallStudent2);
    borders.Add(borderMainHallAssistant2);
    borders.Add(borderMainHallStudent3);
    borders.Add(borderMainHallAssistant3);
    borders.Add(borderClass1Student1);
    borders.Add(borderClass1Assistant1);
    borders.Add(borderClass1Student2);
    borders.Add(borderClass1Assistant2);
    borders.Add(borderClass1Student3);
    borders.Add(borderClass1Assistant3);
    borders.Add(borderClass2Student1);
    borders.Add(borderClass2Assistant1);
    borders.Add(borderClass2Student2);
    borders.Add(borderClass2Assistant2);
    borders.Add(borderClass2Student3);
    borders.Add(borderClass2Assistant3);

    // Loop through the borders
    for(int iBorder = 0; iBorder < borders.Count; iBorder++)
    {
        // Is this border the active student assignment?
        if (comboActiveStudentAssignmentType.SelectedIndex == iBorder)
        {
            // Yes, so use a red …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml code-behind

1
推荐指数
1
解决办法
2310
查看次数

连接没有关闭.连接的当前状态为"打开"

我收到了这个错误.我已经阅读过使用过块的帖子,但我有一个,我仍然得到错误.

我的代码隐藏是:

    protected void btnEdit_OnClick(object sender, EventArgs e)
    {
        MdlCommentsExtender.Enabled = true;
        MdlCommentsExtender.Show();
        Button button = (Button)sender;
        string buttonId = button.ID;
        string[] tokens = buttonId.Split('-');
        ScriptManager.GetCurrent(this).SetFocus(this.txtCommentBox);

        //**************************/
         try
         {

             using (SqlConnection conn = new SqlConnection())
             {
                 conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnCST"].ToString();
                 conn.Open();
                 SqlCommand cmd2 = new SqlCommand();
                 cmd2.Connection = conn;
                 string CmdTxt = "Select CBL.ID, CBL.[Category], CBL.[Provision], CTL.MarkForReview, CTL.IssueType, CTL.Resolution, CTL.Feedback, CTL.TemplateID";
                 CmdTxt = CmdTxt + " from [tblCSTBenefitList] CBL";
                 CmdTxt = CmdTxt + " LEFT JOIN tblCSTTemplateList CTL";
                 CmdTxt = CmdTxt …
Run Code Online (Sandbox Code Playgroud)

c# code-behind

1
推荐指数
1
解决办法
4089
查看次数

ASP.net C# 代码隐藏被认为是服务器端还是客户端?

我想说它只是从我的阅读和理解来看是服务器端的,但我希望得到一些澄清。谢谢。

asp.net webforms server-side client-side code-behind

1
推荐指数
1
解决办法
2920
查看次数

当前上下文中不存在名称“InitializeComponent”

我知道这个错误有很多答案,但它们都是一些系统错误或类似的错误。我刚刚创建了这个应用程序,它运行良好。然后我添加了一个视图模型来处理我在注册和登录页面之间的导航,在 login/register.xaml 中我更新了文本以了解我在哪个页面上。但现在InitializeComponent是不被认可。

我只放了注册页面,因为登录是一样的,但登录名是:

using Xamarin.Forms;

namespace Appointments.Views
{
    public partial class RegisterPage : ContentPage
    {
        public RegisterPage()
        {
            InitializeComponent();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和视图模型:

using Appointments.Views;
using MvvmHelpers;
using MvvmHelpers.Commands;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Appointments.ViewModels
{
    public class RegLogViewModel : BaseViewModel
    {   
        public AsyncCommand GoToRegisterCommand;
        public AsyncCommand GoToLoginCommand;
        RegLogViewModel()
        {
            GoToLoginCommand = new AsyncCommand(GoToLogin);
            GoToRegisterCommand = new AsyncCommand(GoToRegister);
        }

        async Task GoToRegister()
        {
            await Shell.Current.GoToAsync($"//{ nameof(RegisterPage)}");
        }

        async Task GoToLogin()
        { …
Run Code Online (Sandbox Code Playgroud)

c# xaml code-behind xamarin xamarin.forms

1
推荐指数
1
解决办法
50
查看次数