在C#中动态定位HTML按钮

时间:2022-09-20 13:14:38

I know this should be real simple, but I have googled this problem and I do not see the same available properties for my button. What I have googled says I should be able to change an HTML button's location with the Location property. However, this is not an option for me. How do I change the button's location dynamically in C#? Here is the relevant code in the ASPX.CS file:

我知道这应该是非常简单,但我已经搜索了这个问题,我没有看到我的按钮可用的相同属性。我用google搜索说我应该可以使用Location属性更改HTML按钮的位置。但是,这对我来说不是一个选择。如何在C#中动态更改按钮的位置?以下是ASPX.CS文件中的相关代码:

protected void btnSubmit_Click(object sender, System.EventArgs e)
{
    int cnt = FindOccurence("DropDownListID");
    AppendRecords();
    pnlDisplayData.Visible = false;
    btnSubmit.Visible = false;
    resultLabel.Attributes.Add("style", "align=center"); 
    resultLabel.Visible = true;
}

I want to reposition btnSubmit. In the ASPX file this button is defined as:

我想重新定位btnSubmit。在ASPX文件中,此按钮定义为:

<asp:button id="btnSubmit" runat="server" text="Submit" width="150px" 
style="top:auto; left:auto"
OnClick="btnSubmit_Click"></asp:button>

2 个解决方案

#1


The only thing wrong with your code I can see at the moment is that this line:

我现在能看到的代码唯一不对的是这一行:

resultLabel.Attributes.Add("style", "align=center");

should read:

resultLabel.Attributes.Add("style", "align:center");

CSS properties are done like:

CSS属性完成如下:

property:value;

NOT:

property=value;

#2


set style on control using Style collection, this will add properly style to existing styles on control defined inline:

使用Style集合在控件上设置样式,这将为内联定义的控件上的现有样式添加适当的样式:

resultLabel.Style.Add("align", "center");
btnSubmit.Style.Add("top", "auto");
btnSubmit.Style.Add("left", "auto");

setting exact absolute location of button:

设置按钮的确切绝对位置:

btnSubmit.Style.Add("position", "absolute");
btnSubmit.Style.Add("top", "10");
btnSubmit.Style.Add("left", "10");

#1


The only thing wrong with your code I can see at the moment is that this line:

我现在能看到的代码唯一不对的是这一行:

resultLabel.Attributes.Add("style", "align=center");

should read:

resultLabel.Attributes.Add("style", "align:center");

CSS properties are done like:

CSS属性完成如下:

property:value;

NOT:

property=value;

#2


set style on control using Style collection, this will add properly style to existing styles on control defined inline:

使用Style集合在控件上设置样式,这将为内联定义的控件上的现有样式添加适当的样式:

resultLabel.Style.Add("align", "center");
btnSubmit.Style.Add("top", "auto");
btnSubmit.Style.Add("left", "auto");

setting exact absolute location of button:

设置按钮的确切绝对位置:

btnSubmit.Style.Add("position", "absolute");
btnSubmit.Style.Add("top", "10");
btnSubmit.Style.Add("left", "10");