如何在多视图控件中单独更新不同的视图

时间:2016-06-02 14:02:42

标签: c# asp.net

我创建了一个具有不同视图的多视图控件。每个视图都有单独的编辑按钮,这些按钮应仅更新该视图中的字段。编辑按钮在第一个视图/选项卡上正常工作。但不是在其他人。我在不同视图中的不同表中使用了更新面板。还是行不通。打击就是代码。

发现帽子代码无法在表格内容中找到任何控件。但为什么呢?

<asp:Button Text="Information" BorderStyle="None" ID="Tab1" CssClass="Initial" runat="server" OnClick="Tab1_Click" />
<asp:Button Text="IP Adresses" BorderStyle="None" ID="Tab2" CssClass="Initial" runat="server" OnClick="Tab2_Click" />

<asp:MultiView ID="MainView" runat="server">
  <asp:View ID="View1" runat="server">
      <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
           <ContentTemplate>
            <div id="div1" runat="server" >
                <table id="tableContentInfo" style="width: 80%;"  runat="server"></table>
            </div>
          </ContentTemplate>
      </asp:UpdatePanel>
  </asp:View>   
  <asp:View ID="View2" runat="server">
       <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
           <ContentTemplate>
              <div id="div2" runat="server" >
                 <table id="tableContentMake_Model" style="width: 100%;"  runat="server"></table>
              </div>
           </ContentTemplate>
       </asp:UpdatePanel>
  </asp:View>
</asp:MultiView>

这是后面的代码..视图中的控件是动态创建的,它们正在显示数据。 tableContentMake_Model是第二个视图ID。

string strServer = "";
        protected void Page_Init(object sender, EventArgs e)
        {
            GetDataFromSession();
            lblName.Text = strServer;
            LoadViewInfo(strServer);
            LoadViewMake_Model(strServer);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Tab1.CssClass = "Clicked";
            Tab2.CssClass = "Initial";
            Tab3.CssClass = "Initial";
            Tab4.CssClass = "Initial";
            Tab5.CssClass = "Initial";
            Tab6.CssClass = "Initial";
            MainView.ActiveViewIndex = 0;  

        }
protected void Tab1_Click(object sender, EventArgs e)
        {
            Tab1.CssClass = "Clicked";
            Tab2.CssClass = "Initial";
            Tab3.CssClass = "Initial";
            Tab4.CssClass = "Initial";
            Tab5.CssClass = "Initial";
            Tab6.CssClass = "Initial";
            MainView.ActiveViewIndex = 0;
            GetDataFromSession();
            LoadViewInfo(strServer);

        }

        protected void Tab2_Click(object sender, EventArgs e)
        {
            Tab1.CssClass = "Initial";
            Tab2.CssClass = "Clicked";
            Tab3.CssClass = "Initial";
            Tab4.CssClass = "Initial";
            Tab5.CssClass = "Initial";
            Tab6.CssClass = "Initial";
            MainView.ActiveViewIndex = 1;
            GetDataFromSession();
            LoadViewMake_Model(strServer);
        }
private void button_Edit_MakeModel_Click(object sender, EventArgs e)
        {
            foreach (HtmlTableRow row in tableContentMake_Model.Rows)
            {
                foreach (HtmlTableCell cell in row.Cells)
                {
                    foreach (Control ctrl in cell.Controls)
                    {
                        if (ctrl.GetType() != typeof(LiteralControl))
                        {
                            if (ctrl is HtmlInputCheckBox)
                            {
                                HtmlInputCheckBox chk = (HtmlInputCheckBox)ctrl;
                                chk.Disabled = false;
                            }
                            if (ctrl is HtmlInputText)
                            {
                                HtmlInputText txt = (HtmlInputText)ctrl;
                                txt.Attributes.Remove("readonly");
                                txt.Style.Add("border", "1px solid #DBE0E4");
                            }
                            if (ctrl is HtmlTextArea)
                            {
                                HtmlTextArea txtarea = (HtmlTextArea)ctrl;
                                txtarea.Attributes.Remove("readonly");
                                txtarea.Style.Add("border", "1px solid #DBE0E4");
                            }
                        }
                    }
                }
            }
            Button btnEdit = (Button)tableContentMake_Model.FindControl("Edit_MakeModel");
            btnEdit.Visible = false;
            Button btnSave = (Button)tableContentMake_Model.FindControl("Save_MakeModel");
            btnSave.Visible = true;
            Button btnCancel = (Button)tableContentMake_Model.FindControl("Cancel_MakeModel");
            btnCancel.Visible = true;


        }

提前感谢您的帮助。

这是按钮代码的创建..

int ct = 0;
            int nullct = 0;
            string fields = "";
            string strControl = "";
            tableContentMake_Model.Rows.Clear();

            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["WebTeamServersConnectionString"].ConnectionString);
            string sCommand = "select * from server_List_Choices where ListHeading='Make_Model'";
            SqlCommand command = new SqlCommand(sCommand, cn);
            cn.Open();
            SqlDataReader reader = command.ExecuteReader();
            HtmlTableRow row = new HtmlTableRow();
            HtmlTableCell cell = new HtmlTableCell();
            while (reader.Read())
            {
                if (ct == 0)
                {
                    cell.InnerText = reader.GetValue(3).ToString().Trim();
                    cell.Width = "50px";
                    cell.Style.Add("font-weight", "bold");
                    cell.Style.Add("color", "#69be28");
                    row.Cells.Add(cell);
                    fields = reader.GetValue(2).ToString().Trim();
                    strControl = reader.GetValue(4).ToString().Trim();
                }
                else
                {
                    cell = new HtmlTableCell();
                    cell.InnerText = reader.GetValue(3).ToString().Trim();
                    if (reader.GetValue(3).ToString().Trim() == "Operating System")
                    {
                        cell.Width = "150px";
                    }
                    else
                    {
                        cell.Width = "40px";
                    }
                    cell.Style.Add("font-weight", "bold");
                    cell.Style.Add("color", "#69be28");
                    row.Cells.Add(cell);
                    fields = fields + "," + reader.GetValue(2).ToString().Trim();
                    strControl = strControl + "," + reader.GetValue(4).ToString().Trim();
                }
                ct = ct + 1;
            }

            tableContentMake_Model.Rows.Add(row);
            cn.Close();

            SqlConnection cn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["WebTeamServersConnectionString"].ConnectionString);
            string sCommand1 = "select " + fields + " from [Web Team Servers] where Server='" + server + "'";
            SqlCommand command1 = new SqlCommand(sCommand1, cn1);
            cn1.Open();
            SqlDataReader reader1 = command1.ExecuteReader();
            row = new HtmlTableRow();

            while (reader1.Read())
            {
                for (int i = 0; i < reader1.VisibleFieldCount; i++)
                {
                    cell = new HtmlTableCell();
                    if ((reader1.GetValue(i).ToString().Trim() == "") || (reader1.GetValue(i).ToString().Trim() == null))
                    {
                        nullct = nullct + 1;
                    }
                    string[] s = strControl.Split(',');
                    string p = s[i];
                    switch (p)
                    {
                        case "chk":
                            HtmlInputCheckBox checkbox = new HtmlInputCheckBox();
                            if (reader1.GetValue(i).ToString().Trim() == "Y")
                            {
                                checkbox.Checked = true;
                            }
                            else
                            {
                                checkbox.Checked = false;
                            }
                            checkbox.Disabled = true;
                            cell.Controls.Add(checkbox);
                            row.Cells.Add(cell);
                            break;
                        case "txt":
                            string valuetxt = reader1.GetValue(i).ToString().Trim();
                                HtmlInputText textbox = new HtmlInputText();
                                textbox.Value = valuetxt;
                                textbox.ID = "txt" + reader1.GetName(i).ToString().Trim();
                                textbox.Attributes.Add("readonly", "readonly");
                                textbox.Style.Add("border", "none");

                                if (reader1.GetName(i).ToString().Trim() == "OperatingSystem")
                                {
                                    textbox.Style.Add("width", "300px");
                                }
                                else
                                {
                                    textbox.Style.Add("width", "100px");
                                }
                                cell.Controls.Add(textbox);

                                break;
                        case "txtarea":
                                string value = reader1.GetValue(i).ToString().Trim();

                                value = value.Replace("\"", "");
                                value = value.Replace("\n", "");
                                value = value.Replace("\r", "");
                                value = value.Replace("~", "\n");

                                HtmlTextArea testarea = new HtmlTextArea();
                                testarea.Value = value;
                                testarea.ID = "txtarea" + reader1.GetName(i).ToString().Trim();
                                testarea.Attributes.Add("readonly", "readonly");
                                testarea.Style.Add("border", "none");
                                testarea.Style.Add("height", "50px");
                                cell.Controls.Add(testarea);
                                cell.Width = "30px";
                                break;
                        default :
                            break;
                    }
                    cell.Style.Add("color", "black");
                    row.Cells.Add(cell);

                }
                if (nullct < reader1.VisibleFieldCount)
                {
                    cell = new HtmlTableCell();
                    Button button = new Button();
                    button.Text = "Edit";
                    button.ID = "Edit_MakeModel";
                    button.Click += new EventHandler(button_Edit_MakeModel_Click);
                    cell.Controls.Add(button);
                    cell.Width = "50px";
                    row.Cells.Add(cell);

                    cell = new HtmlTableCell();
                    Button button1 = new Button();
                    button1.Text = "Save";
                    button1.ID = "Save_MakeModel";
                    button1.Visible = false;
                    button1.Click += new EventHandler(button_Save_MakeModel_Click);
                    cell.Controls.Add(button1);
                    cell.Width = "50px";
                    row.Cells.Add(cell);

                    cell = new HtmlTableCell();
                    Button button2 = new Button();
                    button2.Text = "Cancel";
                    button2.ID = "Cancel_MakeModel";
                    button2.Visible = false;
                    button2.Click += new EventHandler(button_Cancel_MakeModel_Click);
                    cell.Controls.Add(button2);
                    cell.Width = "50px";
                    row.Cells.Add(cell);

                }
                tableContentMake_Model.Rows.Add(row);
            }
            cn1.Close();

2 个答案:

答案 0 :(得分:0)

解决了问题......

protected void Page_Load(object sender, EventArgs e)
        {
switch (strClicked)
            {
                case "Tab1" :
                    Tab1.CssClass = "Clicked";
                    Tab2.CssClass = "Initial";
                    Tab3.CssClass = "Initial";
                    Tab4.CssClass = "Initial";
                    Tab5.CssClass = "Initial";
                    Tab6.CssClass = "Initial";
                    MainView.ActiveViewIndex = 0;
                    break;
                case "Tab2" :
                    Tab1.CssClass = "Initial";
                    Tab2.CssClass = "Clicked";
                    Tab3.CssClass = "Initial";
                    Tab4.CssClass = "Initial";
                    Tab5.CssClass = "Initial";
                    Tab6.CssClass = "Initial";
                    MainView.ActiveViewIndex = 1;
                    break;
                case "Tab3" :
                    Tab1.CssClass = "Initial";
                    Tab2.CssClass = "Initial";
                    Tab3.CssClass = "Clicked";
                    Tab4.CssClass = "Initial";
                    Tab5.CssClass = "Initial";
                    Tab6.CssClass = "Initial";
                    MainView.ActiveViewIndex = 2;
                    break;
                default :
                    break;

            }
}

答案 1 :(得分:0)

我认为你可以为多视图控件之上的所有共同视图设置一个更新面板。我通常以这种方式使用。希望可以提示你的问题。

如果您的问题得到解决,请告诉我。

由于