GridView in the data editor to open a new page of & add controls dynamically

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

1,

GridView binding to data has very powerful function, is also very easy to use, we often used to bind data, we also need to maintain the data, usually put a template column in GridView, the template column on a button, click the button, in the GridView RowCommand event through the Response.Redrict () to jump to another page editing. For example:

 <asp:TemplateField HeaderText="Administration">

<ItemTemplate>

      <asp:ImageButton ID="imgBtnManage" runat="server" CommandName="manage"   ImageUrl="~/Images/manage_25X25.gif"           

        CommandArgument='<%# Eval("ProductID") %>' />

  </ItemTemplate>

</asp:TemplateField>


And then in the Row_Command () in the event of operation

 protected void gvProduct_RowCommand(object sender, GridViewCommandEventArgs e)

{

string strProductId = e.CommandArgument.ToString();

     int index = ((GridViewRow)((ImageButton)(e.CommandSource)).Parent.Parent).RowIndex;//Gets the current line number on the front

    if (e.CommandName == "manage")

    {

      Response.Redirect("xx.aspx?id="+strProductId);

    }

}


2,

We can also use the following method, i.e. template list in GridView in a Html control, as follows: put a Image, use the onclick event, using JS to achieve, JS function can be used in the form of pass parameters,

**Note, this place is Image, the Html control, if you are going through data binding approach that the <%#; Eval ("*")%> this way to the JS function parameters, using HTML control events only, not for server controls (including the ImageButton OnClientClick event is not)

<asp:TemplateField HeaderText="Administration">

  <ItemTemplate>

     <img id="imgManage" src="Images/manage_25X25.gif"CategoryID") %>','<%#Eval("ProductId") %>')" />

  </ItemTemplate>

</asp:TemplateField>

 If you want to use a server control, and the fields that you want to bind the data source, the onclick event, must be in the background after the bound data source controls are added, such as: Attributes.Add

((Button)e.Row.FindControl("btnEdit")).Attributes.Add("onclick", "return UniversalOpenWindowAndBreak(720,640,'NewStudentIssue.aspx?issueId=" + issueId + "');");

3,

Also can use the following ways script parameters, which can function on the open dialog box or window

<asp:TemplateField HeaderText="Administration">

  <ItemTemplate>

     <img onclick=="UniversalOpenWindow(650,450,'ScanOrgInfo.aspx?classid=<%#Eval("ClassID") %>')" />

  </ItemTemplate>

</asp:TemplateField>


Can also add event, add client in the pages of CS code in the event

/// <summary>

/// Each line of linkButton GridView add a client's onclick event, open a view or the modal window

/// </summary>

/// <param name="ds">GridView bound to the data source</param>

private void AddEvent(DataSet ds)

{

if (ds != null && ds.Tables[0].Rows.Count > 0)

{

foreach (GridViewRow gr in gvClassInfoList.Rows)

{

LinkButton lkBtn = (LinkButton)gr.Cells[15].FindControl("lkBtnLookOrg");//dgOtherList.Items[i].Cells[2].FindControl("lnkPN");

//string str = ((Label)gr.Cells[0].FindControl("lblText")).Text;

string classid = gr.Cells[0].Text.ToString();

lkBtn.Attributes.Add("onclick", "return UniversalOpenWindow(660,450,'NewClass.aspx?classid='"+classid+")");

}

}

}

JS can be as follows, as you write:

<script type="text/javascript">

function NavWindow(currId,productId)

{

var strUrl = "AddProduct.aspx?currId="+currId +"&productId="+productId;//document.getElementById('btnAddProduct').getAttribute("cateid");

window.open(strUrl);

event.returnValue=false;//This is to prevent the client events back, were submitted to PostBack

return false;

}

//Open the modal and non modal dialog box

function UniversalOpenWindow(w,h,fileName)

{

   window.showModalDialog(fileName, argsObjectModalWindow, "dialogWidth:" + w + "px; dialogHeight:" + h + "px; center:Yes; help:No;

     resizable:No; scroll:no; status:No")

}

function UniversalOpenWindowFree(w,h,fileName)

{

   window.showModelessDialog(fileName, argsObjectModalWindow, "dialogWidth:" + w + "px; dialogHeight:" + h + "px; center:Yes; help:No;

    resizable:Yes; scroll:No; status:No")

}

</script>

http://www.cnblogs.com/vihone/archive/2009/12/30/1636474.html



Asp.Net dynamically add controls, and obtains its value



For most of the time, we in the development of the times, need to dynamically add controls, including Html control, Asp.Net control.

JS generates the HTML control

function AddProperty()

{

var count = document.getElementById('hidCount').value;

var str = "<BR> Property name: <input type=\ "Text\" id=\ "txtProperty" +count+ "," name=\ "txtProperty" +count+ "," /> & nbsp; & nbsp; attribute description:<input type=\"Text\" id=\"txtDesc"+count+"\" name=\"txtDesc"+count+"\" />";

count++;

document.getElementById('hidCount').value=count;

document.getElementById('catePro').innerHTML+=str;

}


As for production controls to take its value, therefore, must specify its ID, can put a HiddenField control on the page, and specify the default value is 0; when to add controls dynamically, can through the JS script to change its value, the script above.

<asp:HiddenField ID="hidCount" Value="0" runat="server" />


In the background, can pass the following means to take its value, as follows:

 int count = int.Parse(this.hidCount.Value);//Have a number of control is generated, stored in the page of HiddenField, and the ID

for (int i = 0; i <count; i++)

{

string propName = string.Empty;

string propDesc = string.Empty;

if (!string.IsNullOrEmpty(Request["txtProperty" + i]))//To determine whether there is

{

PIM.Model.CateProperty model = new PIM.Model.CateProperty();

propName = Request["txtProperty" + i].ToString();//, The value of the control that has generated

propDesc = Request["txtDesc" + i].ToString();

//......

}

 }


Two, the dynamic generation of Asp.Net server controls, and its value


private void LoadProperty(int cateId)

HtmlGenericControl span = new HtmlGenericControl();

 for (int i = 0; i <ds.Tables[0].Rows.Count; i++)

{

StringBuilder strHtml = new StringBuilder();

if (i % 2 == 0)

{

strHtml.Append("<div >");

strHtml.Append("<div class=\"layout1\">");

}

else

{

strHtml.Append("<div >");

}

TextBox txt = new TextBox();

Label lbl = new Label();

HiddenField hdf = new HiddenField();//

lbl.ID = "lblProp" + i.ToString();

lbl.Text = ds.Tables[0].Rows[i]["PropName"].ToString() + ":";

txt.ID = "txtProp" + i.ToString();

hdf.ID = "hdfProp" + i.ToString();//

lbl.EnableViewState = true;

txt.EnableViewState = true;

hdf.EnableViewState = true;//

LiteralControl lc = new LiteralControl(strHtml.ToString());

lc.EnableViewState = true;

span.Controls.Add(lc);

span.Controls.Add(lbl);

span.Controls.Add(txt);

span.Controls.Add(hdf);//

if (i % 2 == 0)

{

span.Controls.Add(new LiteralControl("</div></div>"));

}

else

{

span.Controls.Add(new LiteralControl("</div>"));

}

}

 }

Dynamically generated controls, when clicking on the button will refresh the page, so the controls do not need, generating method

if(!IsPostBack)

{

}


If in the PageLoad method, even though every time dynamic loading are unable to get the data, and dynamically generated controls will disappear, so, according to the Asp.Net life cycle, we will generate the control in the OnInit () method,

The override this method

  protected override void OnInit(EventArgs e)

{

 LoadProperty(cateID);//Method to add controls dynamically execution in OnInit, it will save its state view

 }

If these dynamically generated controls, in the generation time, requires the values from the database, and set its value, can do an assignment in PageLoad,

Then you can use the (IsPostBack!), and will not lose its value.

 protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

 if (!string.IsNullOrEmpty(Request["productId"]))

{

string productId = Request["productId"].ToString();

int parentId = int.Parse(ParentID);

LoadData(productId, parentId);//Can the assignment method... Realization of ellipsis.

 }

}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Harlan at November 17, 2013 - 9:23 PM